Thinkphp empty operation, empty controller, namespace (detailed description), thinkphp namespace

Source: Internet
Author: User

Thinkphp empty operation, empty controller, namespace (detailed description), thinkphp namespace

1. Empty operation

When the system cannot find the requested operation method, the system locates the null operation (_empty) Method. With this mechanism, we can optimize error pages and URLs.

Http: // URL/index. php/Home/Main/login

Http: // URL/index. php/Home/Main/Hello null operation

Page:

The error message displayed is too detailed. For the sake of security and page optimization, empty operations are performed.

1. Create a _ empty () method. It should be written in the subclass and not in the parent class (recommended)

Function _ empty () {echo "the webpage does not exist. Check the browser address! ";}

2. Create a template corresponding to this method

Create a new text.html file under view/main

<Body> 

Note: The null operation method is valid only when your Controller class inherits the system's Think \ Controller class. Otherwise, you must define__call.

2. Empty Controller

The concept of an empty controller is that when the system cannot find the requested controller name, the system will try to locate the empty controller (EmptyController ), this mechanism can be used to customize error pages and optimize URLs.

Home/Controller/EmptyController. class. php

<? Phpnamespace Home \ Controller; use Think \ Controller; class EmptyController extends Controller {public function _ empty () {echo "The accessed page does not exist! ";}}

3. Cross-controller call

When executing a controller, You Can instantiate another controller and access its specified method through an object.

Cross-controller call can save the workload of our code

3.1 In the same module:

For example, the Home/Controller has two controllers: IndexController. class. php and MainController. class. php.

There is a method in IndexController. class. php:

Public function ShuChu () {return "educational administration system! ";}

Now we want to call the ShuChu () method in MainController. class. php.

Function DiaoYong () {// Method 1: Create an object // $ index = new IndexController (); // echo $ index-> ShuChu (); // create an object with A shortcut function: // A ("[module/] Controller flag") instantiate the Controller object // Method 2: Use () method // $ index = A ("Index"); // echo $ index-> ShuChu (); // R ([module/] Controller flag/Operation Method) instantiate an object and call the specified method at the same time. // method 3: Use the R () method to create an object and call a specific method echo R ("Index/ShuChu ");}

Running result:

3.2 different modules:

Add a new module Admin

TextController. class. php:

<?phpnamespace Admin\Controller;use Think\Controller;class TextController extends Controller{    public function Text()  {    echo "HHHHH";  }}

Now you want to call the Text () method in Home/Controller/MainController. class. php

Function Text () {// cross-module call // Method 1: Use the () method // $ tt = A ("Admin/Text "); // echo $ tt-> Text (); // Method 2: Use the R () method, create an object and call a specific method echo R ("Admin/Text ");}

Running result:

4. namespace

It is equivalent to a virtual directory.

Use folders for normal file management-physically differentiate

The initial namespace of the TP framework is ThinkPHP \ Library.

In the namespace of the TP framework, "\" represents the initial namespace (ThinkPHP \ Library)

1. The root namespace under the system directory is named by the folder name under ThinkPHP \ Library

2. The root namespace of the module is named after the module name.

When defining a namespace, you need to write it from the root, for example, Home \ Controller, for example, Think \ Model.

If you want to use a class file, when introducing the class namespace:

Use the use keyword and add "\ file name" to the namespace of the class"

If you do not want to introduce a namespace, you can use \ (initial namespace) to find the corresponding class when creating the class object.
Example: new \ Think \ Page (30 );

The above Thinkphp empty operation, empty controller, and namespace (detailed description) are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.

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.