Getting Started with Thinkphp 2 (46)

WBOY
Release: 2016-08-08 09:22:57
Original
1020 people have browsed it

【No operation processing】

Look at the following picture:

Actual situation: Our User controller does not have the hello() method

If an object accesses a method that does not exist in this class, then it will access it." Magic method __call()"

  The user accesses a non-existent operation - "Solution: Define an _empty() method for each controller to handle

  The second solution: Define an empty operation

【Empty module processing】

We use a class, but this class has not been included yet.

We can handle __autoload() through the automatic loading mechanism. If the automatic loading mechanism does not find this class, an error will be reported.

That is, request an empty module

Solution one: define an empty controller, empty module

Solution two:

【Add function library file to the application】

In the Common folder Inside, is the place where our function library files are placed

[Module Grouping]

 1.Controller for grouping settings

 2.View templates need to be grouped

 3. Configuration variables need to be grouped

  4. Configure config.php

【Pre-operation, post-operation】

When the method in the controller is called , you can do some additional work before or after the call, which is called pre-operation and post-operation

When we request this URL: http://url/index.php/Admin/Goods/zhanshi, When the method exists, where does this action occur? The pre-operations in the exec() method of App.class.php

can be executed first when zhanshi() is executed. The post-operation can be executed after zhanshi() is executed

There are many methods in a class, all of which require pre- and post-operations. How to solve it?

Solution:

[Cross-module call]

Instantiating a non-existent class will automatically load the class through __autoload().

Where is __autoload() of tp framework? In Think.class.php

instantiate the controller object through the A() method

 A('Module Controller') For example: A('Goods')

 A('Group/Controller') For example: A ('home/Ucenter');

A('project://group/controller') For example: A('shop://home/Ucenter');

There is a nested import call in the A() method () method, this method helps us obtain the corresponding controller and require it to be introduced. The A() method directly new instantiates the object

R ("project://group/controller/operation") method

 which inherits the introduction of the controller, instantiated objects, and method calls.

  R('Module Controller/Operation') For example: A('Goods')

R('Group/Controller/Operation') For example: A('home/Ucenter/members');

R( 'Project://group/controller/operation') For example: A('shop://home/Ucenter/members');

Call the A() method in the R() method

Call in the A() method import() method

 A() method is used

 R() method is used

The above introduces Getting Started with Thinkphp Part 2 (46), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!