Thinkphp Learning-Controller-controller definition

Source: Internet
Author: User

Generally speaking, the thinkphp controller is a class, and the operation is a public method of the controller class.

The following is a typical definition of a controller class:

<? phpnamespace Home\controller;  Use Think\controller; class extends Controller {    publicfunction  hello () {        echo ' Hello ', thinkphp! ' ;    }}

Home\IndexControllerThe class represents the index controller under the home module, and the Hello operation is Home\IndexController the Hello (public) method of the class.

When accessed http://serverName/index.php/Home/Index/hello , it outputs:

hello,thinkphp!

Note: If you set the action method to bind to a class, the action method corresponds to a class (the reference operation is bound to the class).

Defining the Controller

A controller usually needs to inherit the controller class or its subclasses of the system, for example, the following defines a \Home\Controller\IndexController class of controllers:

<? phpnamespace Home\controller;  Use Think\controller; class extends Controller {    publicfunction  hello () {        echo ' Hello ';    }      Public function Test () {        echo ' test ';    }}

The name of the controller is named with the Hump (capitalized) and the controller file is located Home/Controller/IndexController.class.php .

The Hello and test method of the Indexcontroller controller class is the action method, which accesses the following URL address:

http://Servername/home/index/hellohttp://servername/home/index/test

will be output separately:

Hello // and Test

The operation method must be defined as a public method, otherwise the operation error is reported, so the following operation definition can only access the Hello operation and cannot access the test operation.

<? phpnamespace Home\controller;  Use Think\controller; class extends Controller {    publicfunction  hello () {        echo ' Hello ';    }     protected function Test () {        echo ' test ';    }}

Because the operation method is a controller of a method, so encountered with the system of the keyword conflict method may not be defined, this time we can set the operation method suffix to solve, for example:

' Action_suffix '         = +  //  operation method suffix

Set the action method suffix to action so that the Controller's action method definition is adjusted to:

<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionlistaction () {Echo' List '; }     Public functionhelloaction () {Echo' Hello '; }     Public functiontestaction () {Echo' Test '; }}

The suffix setting of the action method only affects the definition of the Controller class, and has no effect on URL access.

Multilayer Controller

The thinkphp controller supports multilayer and multilevel, and multi-layer refers to the controller can be layered, for example, in addition to the default controller layer (which we can call the access controller), but also add the event controller (layer), for example:

├─controller Access Controller │  ├─usercontroller. class. php│  ├─blogcontroller. class. php│   ... ├─event Event Controller │  ├─userevent. class. php  │  ├─blogevent.  Class. php│   ...

The name of the access controller is set by Default_c_layer , which is the default.

The access controller is responsible for the external interaction response, through URL request responses, for example http://serverName/Home/User/index , while the event controller is responsible for internal event response and can only be called internally, so it is isolated from the outside.

The Division of multi-layer controllers can be layered according to the needs of the project.

If you are defining a different controller layer, you do not necessarily have to inherit the controllers class or its subclasses of the system, and it is usually necessary to inherit the controller class when you need to export the template. For example:

<? phpnamespace home\event; class userevent {    publicfunction  login () {        echo ' login event ';    }      Public function logout () {        echo ' logout event ';    }}

The Userevent event controller is located Home/Event/UserEvent.class.php .

Multilevel Controller

Multi-level controller refers to the controller can be a sub-directory of a controller layer group storage, first need to set the controller hierarchy, for example, we set the Level 2 directory controller layer:

' Controller_level '      =  2,

The location of the controller files is placed as follows:

├─controller Access Controller │  ├─user User Rating (group) │  │  ├─usertypecontroller.  Class. php│  │  ├─userauthcontroller.  Class. php│   ... │  ├─admin Admin Rating (group) │  │  ├─usercontroller.  Class. php│  │  ├─configcontroller.  Class. php│   ...

Namespaces in a multilevel controller need to be defined like this:

<? phpnamespace home\controller\admin;  Use Think\controller; class extends Controller {    publicfunction  hello () {        echo ' Hello ';    }      Public function Test () {        echo ' test ';    }}

You can then access it via the URL address:

http://servername/home/user/usertypehttp://servername/home/admin/user

If you want to simplify the module address in the URL address, refer to the module deployment

Instantiating a controller

The instantiation of the access controller is usually done automatically, and the system is automatically instantiated based on the URL address of the controller to be accessed, and the associated action method is called.

If you need to call across a controller, you can instantiate it separately:

// instantiate the user controller for the home module $User New \home\controller\usercontroller (); // Instantiate the Admin module's blog controller $Blog new \admin\controller\blogcontroller ();

The system provides a quick call method A for the above controller instantiation, the above code can be simplified to:

// assuming the current module is the home module $User = A (' User '$Blog = A (' Admin/blog ');

By default, the A method instantiates the default Director layer (Controller), and if you are instantiating another hierarchical controller, you can use:

// Suppose the current module is the Home module//instantiation Event Controller $User = A (' User ', ' event '$Blog = A (' Admin/blog ', ' event ');

The above code is equivalent to:

// instantiate the user event controller for the home module $User New \home\event\userevent (); // Instantiate the Admin module's Blog event controller $Blog new \admin\event\blogevent ();

Thinkphp Learning-Controller-controller definition

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.