thinkphp-NULL module + NULL operation

Source: Internet
Author: User

Empty operation

An empty operation is when the system cannot find the specified action method.
Will navigate to the empty operation (_empty) method to execute, using this mechanism,
We can implement error pages and some URL optimizations.

For example, below we use the empty operation function to implement a city switching function.
We only need to define a _empty (empty operation) method for the Cityaction class:

<?phpclass Cityaction extends action{public    function _empty ($name) {        //The operation of all cities is resolved to the city method        $this, City ($name);    }        Note that the city method itself is the protected method    of protected function City ($name) {        //and $name is related to the processing of the         echo ' current cities '. $name;    }}

 
Next, we can enter

http://serverName/index.php/City/beijing/
in the browser http://serverName/index.php /city/shanghai/
http://serverName/index.php/City/shenzhen/

Because Cityaction does not define Beijing, Shanghai or Shenzhen operation method,
so the system will be located in the empty operation method _empty to parse, the _empty method parameter is the operation name inside the current URL,
so you will see the result is output:

The current city: Beijing
Current City: Shanghai
Current City: Shenzhen






Empty module The concept of the

empty module is that when the system cannot find the specified module name, the
system tries to locate the empty module (emptyaction), which we can use to customize the error page and optimize the URL.

Now let's take the previous requirements further and change the URL from
original http://serverName/index.php/City/shanghai/
to http://serverName/index.php/ shanghai/

In a simpler way, if you follow the traditional pattern, we must define an action class for each city, and
then process it within the index method of each action class. However, if you use the empty module function, this problem can be solved.
We can define a emptyaction class for the project

<?phpclass Emptyaction extends action{public    function Index () {        //According to the current module name to determine the operation of the city to be performed         $cityName = module_name;        $this->city ($cityName);    }    Note that the city method itself is the protected method    of protected function City ($name) {        //and $name is related to the processing of the         echo ' current cities '. $name;    }}


Next, we can enter it in the browser
http://serverName/index.php/beijing/
http://serverName/index.php/shanghai/
http://serverName/index.php/shenzhen/

Since the system does not exist Beijing, Shanghai, or Shenzhen modules,
So it will go to the empty module (emptyaction) to execute, and you will see that the result of the output is:
Current City: Beijing
Current City: Shanghai
Current City: Shenzhen


Empty modules and empty operations can also be used at the same time to accomplish more complex operations.

thinkphp-NULL module + NULL operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.