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:
//?關(guān)閉默認(rèn)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:
//?獲取當(dāng)前請求的根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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)