Yii notes---redirect, yii---redirect_PHP tutorial
Yii notes---redirect, yii---redirect
Yii’s redirect method is defined in CControler and CHttpRequest, and the redirect in CController is called The redirect method in CHttpRequest. What we usually call is the redirect method in CControoler
Definition in framewok/web/CController
<span>1</span> <span>public</span> <span>function</span> redirect(<span>$url</span>,<span>$terminate</span>=<span>true</span>,<span>$statusCode</span>=302<span>) </span><span>2</span> <span>{ </span><span>3</span> <span>if</span>(<span>is_array</span>(<span>$url</span><span>)) </span><span>4</span> <span> { </span><span>5</span> <span>$route</span>=<span>isset</span>(<span>$url</span>[0]) ? <span>$url</span>[0] : ''<span>; </span><span>6</span> <span>$url</span>=<span>$this</span>->createUrl(<span>$route</span>,<span>array_splice</span>(<span>$url</span>,1<span>)); </span><span>7</span> <span> } </span><span>8</span> Yii::app()->getRequest()->redirect(<span>$url</span>,<span>$terminate</span>,<span>$statusCode</span><span>); </span><span>9</span> }
Parameter description:
@url: Specifies the url link that the browser jumps to. If $url is an array, the first element of the array is composed of the controller/method [controller/action], and the remaining parts are regarded as GET parameters. , the name-value pair and the createUrl method is called to generate the url. If it is a string, directly call the redirect method in framework/web/CHttpRequest.php.
@terminate: Whether to terminate the current application after calling redirect.
@statusCode: Represents the HTTP status code, the default is 302 redirect
About the array_splice function: Remove part of the array and replace it with other values. The above array_splice($url,1) means to remove the first element of the $url array and obtain the value of the GET parameter.
<span>array</span> <span>array_splice</span> ( <span>array</span> &<span>$input</span> , int <span>$offset</span> [, int <span>$length</span> = 0 [, <span>mixed</span> <span>$replacement</span> ]] )
About the createUrl function: This function is defined in many places like redirect, in CController.php and CurlManager.php respectively. The final definition is in CurlManager.php.
The following is the definition of createURL in CController:
<span> 1</span> <span>public</span> <span>function</span> createUrl(<span>$route</span>,<span>$params</span>=<span>array</span>(),<span>$ampersand</span>='&'<span>) </span><span> 2</span> <span> { </span><span> 3</span> <span>if</span>(<span>$route</span>===''<span>) </span><span> 4</span> <span>$route</span>=<span>$this</span>->getId().'/'.<span>$this</span>->getAction()-><span>getId(); </span><span> 5</span> <span>elseif</span>(<span>strpos</span>(<span>$route</span>,'/')===<span>false</span><span>) </span><span> 6</span> <span>$route</span>=<span>$this</span>->getId().'/'.<span>$route</span><span>; </span><span> 7</span> <span>if</span>(<span>$route</span>[0]!=='/' && (<span>$module</span>=<span>$this</span>->getModule())!==<span>null</span><span>) </span><span> 8</span> <span>$route</span>=<span>$module</span>->getId().'/'.<span>$route</span><span>; </span><span> 9</span> <span>return</span> Yii::app()->createUrl(<span>trim</span>(<span>$route</span>,'/'),<span>$params</span>,<span>$ampersand</span><span>); </span><span>10</span> }
We can see several situations from here:
1. If redirect does not take parameters and $route is empty, it will be directed to the current method of the current controller $route=$this->getId().'/'.$this->getAction ()->getId();
2. If there is no '/' in $route, for example $this->render('index',array('post'=>$questions)); only connects the method without the controller, the program The current controller method ID will be automatically obtained
3. There is a '/' character in the route, but it is not at the first position, and check whether the current controller is in the module; for example, $this->redirect(array('step/show','id'=> ;1)); In this case, the program will automatically determine whether it is a module. We don’t need to follow the name of the module when calling createUrl. If we call the method in the main controller in the module, we can Add the '/' character to the letter. And the program will remove the / characters before and after $route at the end.
Definition in framework/web/CHttpRequest.php
<span>1</span> <span>public</span> <span>function</span> redirect(<span>$url</span>,<span>$terminate</span>=<span>true</span>,<span>$statusCode</span>=302<span>) </span><span>2</span> <span> { </span><span>3</span> <span>if</span>(<span>strpos</span>(<span>$url</span>,'/')===0 && <span>strpos</span>(<span>$url</span>,'//')!==0<span>) </span><span>4</span> <span>$url</span>=<span>$this</span>->getHostInfo().<span>$url</span><span>; </span><span>5</span> <span>header</span>('Location: '.<span>$url</span>, <span>true</span>, <span>$statusCode</span><span>); </span><span>6</span> <span>if</span>(<span>$terminate</span><span>) </span><span>7</span> Yii::app()-><span>end</span><span>(); </span><span>8</span> }
If the $url parameter of redirect in CController is not an array, this function will be called directly. If $url does not start with '/', it will jump directly. This situation will cause the redirection in the module to fail. So it is recommended to use array as a parameter when calling the redirect method in CController.php
From this we can see that the redirect method ultimately calls the PHP native header function
Yii::app()->end(); directly calls the exit() function of PHP.

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)

Redirects allow you to redirect client browsers to different URLs. You can use it when switching domains, changing website structure, or switching to HTTPS. In this article, I will show you how to redirect to another page using PHP. I'll explain exactly how PHP redirects work and show you what's happening behind the scenes. Learn PHP with Free Online Courses If you want to learn PHP, check out our PHP Basics free online course! PHP Basics Jeremy McPeak October 29, 2021 How do basic redirects work? Before we get into the details of PHP redirection, let’s take a quick look at how HTTP redirection actually works. Take a look at the image below. Let us understand the above screen

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

It's no secret that Internet Explorer has fallen out of favor for a long time, but with the arrival of Windows 11, reality sets in. Rather than sometimes replacing IE in the future, Edge is now the default browser in Microsoft's latest operating system. For now, you can still enable Internet Explorer in Windows 11. However, IE11 (the latest version) already has an official retirement date, which is June 15, 2022, and the clock is ticking. With this in mind, you may have noticed that Internet Explorer sometimes opens Edge, and you may not like it. So why is this happening? exist

PHP domain name redirection is an important network technology. It is a method of redirecting different domain names visited by users to the same main domain name. Domain name redirection can solve problems such as website SEO optimization, brand promotion, and user access, and can also prevent the abuse of malicious domain names. In this article, we will introduce the specific methods and principles of PHP domain name redirection.

PHP domain name redirection is one of the commonly used technologies in website development. Through domain name redirection, users can automatically jump to another URL when visiting one URL, thereby achieving website traffic guidance, brand promotion and other purposes. The following will use a specific example to demonstrate the implementation method of PHP domain name redirection and show the effect. Create a simple PHP file named redirect.php with the following code:

Removing the index.php file from the server is necessary in some cases, perhaps for security reasons or to upgrade the website. Below I will introduce how to remove the index.php file without affecting the normal operation of the website, and provide specific code examples. How to remove the index.php file in the server? First, we need to ensure that there is a default page in the root directory of the website, such as index.html or other homepage files. Then, we need to configure the server

Redirect is a technique often used in web development, which allows us to redirect users from the current URL address to another URL address. In PHP, redirection is implemented through the header() function. The header() function can output HTTP header information, including redirection information. We can redirect the user to another URL address by using the header() function, as shown below: header("Location:http://www.exam

HTTP status code is a kind of status information returned by the web server to the browser. It is expressed in the form of three digits. Among them, status code 302 represents redirection, also known as temporary jump. This article will deeply analyze HTTP status code 302 and discuss its principles and applications. 1. Overview Redirection is an important concept in the HTTP protocol. When the browser sends a request to the server, the server may return a redirection status code to notify the browser that the current request needs to be redirected, that is, the requested resource address is transferred to another location.
