thinkphp Learning Controllers (Controller)

Source: Internet
Author: User

/*
*/

Entry file index.php:

<? PHP Define (' Think_path ', './thinkphp/'); // define the project name and path Define (' App_name ', ' Myapp '); Define (' App_path ', './home/'); Define (' App_debug ',true); // Loading Frame Entry file require (Think_path. " Thinkphp.php ");
index.php

The./home directory is automatically generated after access to the portal file index.php (http://localhost/index.php).

The contents of the index initial page are displayed by the./home/lib/action/indexaction.class.php File:

<?PHP//This class is automatically generated by the system and is intended for testing purposes onlyclassIndexactionextendsAction { Public functionindex () {$this->show (' <style type= "text/css" >*{padding:0; margin:0;} div{padding:4px 48px;} body{background: #fff; font- Family: "Microsoft Jas Black"; Color: #333;} h1{font-size:100px; font-weight:normal; margin-bottom:12px;} p{line-height:1.8em; font-size:36px}&LT;/STYLE&GT;&L T;div style= "padding:24px 48px;" > ); }}
IndexAction.class.php

(i) Call of the controller

Now modify IndexAction.class.php and use the A function to invoke the project controller:

<? PHP class extends Action {    publicfunction  Test () {            $obj = A ("Member");         $obj-User ();    }}    

The A function is equivalent to New,a ("Member"), which means instantiating the Member controller in this project before invoking the user method in the Member controller. User Method (MemberAction.class.php):

<? PHP class extends action{    publicfunction  User () {            $this,display ();}    } 

Because the test action in the Index controller calls the user method of the Member controller (because the user action here is instantiated, so this cannot be an action, but as a method of the class), the $this->display (), so it needs to be in the current action Output template (./home/tpl/index/test.html) in (Test action):

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>Test</title></Head><Body>Welcome to use thinkphp</Body></HTML>
test.html

The PATHINFO mode URL (http://localhost/index.php/index/test, my host address is 127.0.0.26) can access the test action of the index controller

You can use the R function instead of the A function, and the R function can specify the calling method (action) When invoking control, and modify the IndexAction.class.php:

<? PHP class extends Action {    publicfunction  Test () {            $obj = R ("Member/user"  //Use R function to invoke the controller of this project    }}

can achieve the same effect.

(ii) empty controller and air action

The null controller and the empty action can implement error 404 functionality (but not the server Error page processing mechanism, the null controller and the empty action can only handle pages within the MVC framework, and also cannot handle URLs REWRITE custom format pages).

1. Empty controller

When a user accesses a URL that does not have a controller to access, you can use an empty controller, such as Access Http://localhost/index.php/bbs, which does not actually have a controller bbsaction, and the system will give you an error (required in the portal file index.php Turn on define (' App_debug ', true); ):

Now create the Controller class file under the project directory (./home/lib/action) EmptyAction.class.php:

<? PHP class extends action{    publicfunction  index () {            $this->assign ("msg", " The column you are viewing does not already exist ")        ; $this->display ("./public/html/error.html");}    }

At the same time, create error.html under the public/html of the WEB root, so that access Http://localhost/index.php/bbs is the custom information content.

2. Empty action

Compared with the empty controller to locate the column, the empty action is to locate the specific page. If the user action does not exist in the index controller, simply add an empty action to the index controller and the system will replace the _empty action with the user action:

<? PHP class extends Action {    publicfunction  Test () {            $obj = R ("Member/user");    }    publicfunction  _empty () {            $this->assign (" Msg "," the page you are viewing does not exist ")        ; $this->display ("./public/html/error.html");    }}

Reference: "PHP MVC Development Combat"

Thinkphp Learning Controllers (Controller)

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.