search
HomePHP FrameworkThinkPHPHow 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.

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,

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.

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;

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft