thinkphp empty operations, empty controllers, namespaces

Source: Internet
Author: User

1. Empty operation

An empty operation means that the system will navigate to the empty operation () method when it cannot find the requested action method, and _empty with this mechanism we can implement the error page and some URL optimizations.

http +/URL/index.php/home/main/login

http://url/index.php/home/main/hello null operation

Page appears:

The error message displayed is too detailed to implement an empty operation for security and optimization of the page

1, do a _empty () method. To write in a subclass, do not use the parent class (recommended)

function _empty () {  echo ' page does not exist, please check the browser address information! ";}

2. Create a template that corresponds to this method
Create a new text.html file under View/main

<body>

NOTE: The null action method only works if your controller class inherits the Think\controller class of the system, otherwise it needs to be defined __call to implement it.

2. Empty controller

The concept of an empty controller is that when the system cannot find the requested controller name, the system tries to locate the empty controller (Emptycontroller), which we can use to customize the error page and optimize the URL.

home/controller/emptycontroller.class.php

<? phpnamespace Home\controller;  Use Think\controller; class extends controller{    publicfunction  _empty ()    {        echo " The page visited does not exist! ";        }    }

3, cross-controller call

When a controller executes, it can instantiate another control and access its specified method through the object.

Cross-controller calls can save our code's effort

3.1 Under the same module:

For example: There are two controllers in the Home/controller: IndexController.class.php and MainController.class.php

There is a method in IndexController.class.php:

 Public function Shuchu ()    {        return" Educational Management System! ";        }

Now you want to also call the Shuchu () method in MainController.class.php

function Diaoyong ()     {        // Method One: Build object        //$index =new Indexcontroller ();         echo $index->shuchu ();                The object has a shortcut function for us to use:        //a ("[Module/] Controller flag") Instantiate Controller object        //Method Two: Use A () method        //$index =a ("index");        echo $index->shuchu ();                R ([module/] Controller flag/action method)  instantiating an object invokes the specified method        //Method Three: Using the R () method, creating the object and calling a specific method        echo R ("index/ Shuchu ");
}

Operation Result:

3.2 Under different modules:

New Add a module admin

TextController.class.php:

<? phpnamespace Admin\controller;  Use Think\controller; class extends controller{        publicfunction  Text ()    {        echo "Hhhhh" ;    }}

Now you want to also call the text () method in home/controller/maincontroller.class.php

function Text ()    {        // Cross module Call                        //Method One: Use a () method        //$tt =a ("Admin/text");        echo $tt->text ();                Method Two: Use the R () method to create the object and invoke a specific method        echo R ("Admin/text/text");                }

Operation Result:

4. Namespace namespace

Equivalent to a virtual directory


Normal administrative file use folder--Physical distinction


The initial namespace for the TP framework is: thinkphp\library

Within the TP framework the namespace is used \ represents the initial namespace (thinkphp\library)

1. The root namespace under the system directory is named after the Thinkphp\library folder name

2. The module's root namespace is named after the module name

You need to write from the root when defining a namespace for example: Home\controller: Think\model


If you want to use a class file, introduce that class of namespaces:
Using the Use keyword, followed by the namespace after the class is appended with "\ FileName"

If you do not want to introduce namespaces: you can use \ (the initial namespace) to find the corresponding class when creating objects of your class
For example: New \think\page (30);

thinkphp empty operations, empty controllers, namespaces

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.