ThinkPHP is a powerful open source PHP framework that is very popular. In ThinkPHP, you can use the u method to generate a URL address. In this article, we will introduce the usage of u method in ThnikPHP.
1. Generate ordinary URLs
In ThinkPHP, you can use the u method to generate ordinary URLs. We can use it like this:
$url = U('index/user');//生成地址为/Home/Index/user
In the above code, we use the U method to generate a corresponding URL address. Among them, index represents the controller name, and user represents the method name. If no module name is specified, it defaults to the current module.
If you want to specify a module name, you can use it like this:
$url = U('Admin/index/user');//生成地址为/Admin/Index/user
In the above code, we use the Admin module instead of the default module.
2. Generate URL with parameters
If you need to add parameters to the URL, you can provide the parameters as an array. For example:
$data = array( 'id' => 1, 'name' => '张三' ); $url = U('index/user',$data);
In the above code, we provide an associative array $data, which contains two parameters, id and name. These parameters are automatically added to the URL.
In the controller, you can use the following code to obtain these parameters:
$id = I('get.id'); $name = I('get.name');
Among them, the I function is the input receiving function in ThinkPHP, which can be used to obtain the parameters in the URL.
3. Generate absolute URL
If you need to add a domain name to the URL, you can use the ABS parameter. For example:
$url = U('index/user',array('id'=>1),'',true);
In the above code, the fourth parameter true is used to generate an absolute URL. This adds the current website's domain name to the beginning of the URL.
4. Generate URL with anchor point
Sometimes we need to specify the anchor point of the page, and we can use the ANCHOR parameter. For example:
$url = U('index/user',array('id'=>1),'',false,'#hash_id');
In the above code, the fourth parameter false is used and the anchor #hash_id is added in the fifth parameter.
5. Summary
In this article, we have learned how to use the u method under the ThinkPHP framework, including generating ordinary URLs, generating URLs with parameters, generating absolute URLs and generating anchor points. URL. Use these tips to do your development work better.
The above is the detailed content of Let's talk about how to use the u() method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!