Home > PHP Framework > ThinkPHP > body text

How to write thinkphp url without backslash

PHPz
Release: 2023-04-11 15:32:18
Original
719 people have browsed it

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
Copy after login

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.

Method 1: Modify the configuration file

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,
Copy after login

After modification, we can regenerate the URL address to test:

// 生成 URL 地址
$url = url('Index/index');
echo $url;  // 输出:/index.phpIndexindex.html
Copy after login

As you can see, the URL address generated at this time no longer has backslashes.

Method 2: Manually splice URL addresses

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template