Home > Article > PHP Framework > How to write thinkphp url without backslash
In the process of using ThinkPHP for website development, the URL address generation function is often used. By default, ThinkPHP will add a backslash between the controller name and the method name when generating the URL address, for example:
// 生成 URL 地址 $url = url('Index/index'); echo $url; // 输出:/index.php/Index/index.html
However, in some scenarios, we may need to do it without the backslash URL address, such as when performing front-end routing jump. This article will introduce how to generate URL addresses without backslashes in ThinkPHP.
We can modify the configuration file of ThinkPHP to make the URL address without backslashes. In the config.php
configuration file, find the url_common_param
item and set it to false. The sample code is as follows:
// 关闭默认URL模式中控制器与操作之间的分隔符“/” 'url_common_param' => false,
After modification, we can regenerate the URL address to test:
// 生成 URL 地址 $url = url('Index/index'); echo $url; // 输出:/index.phpIndexindex.html
As you can see, the URL address generated at this time no longer has backslashes.
In addition to modifying the configuration file, we can also manually splice URL addresses to achieve the effect without backslashes. The sample code is as follows:
// 获取当前请求的根URL地址 $baseUrl = request()->root(true); // 获取控制器名和方法名 $controller = request()->controller(); $action = request()->action(); // 拼接URL地址 $url = $baseUrl . '/' . $controller . $action; echo $url;
With the above code, we can manually splice the URL address without backslashes. It should be noted that manually splicing URL addresses may bring some risks and problems, so it needs to be used with caution in actual applications.
In short, the above two methods can generate URL addresses without backslashes in ThinkPHP. In specific applications, we can choose the appropriate method to generate URL addresses based on the actual situation.
The above is the detailed content of How to write thinkphp url without backslash. For more information, please follow other related articles on the PHP Chinese website!