Thinkphp3.2.2 analysis of front-and back-end public class architecture problems _ php instances

Source: Internet
Author: User
This article mainly introduces thinkphp3.2.2 front-end and back-end public class architecture issues, and analyzes in detail the simple call methods of front-end and back-end public classes in the form of instances, which is of great practical value, for more information about thinkphp3.2.2, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:

3.13 many projects used the front and back-end public classes before, and created the Baseaction under lib/action as the public inheritance class. it was found that many people in section 3.2.2 used the call, so that each time they used the call, it was very troublesome, xiao Bian is deliberately lazy. Use the following methods to solve the problem. If you are interested, you can improve it!

Thinkphp3.2.2 creates Application/Common/Controller/BaseController. class. php

The code is as follows:

<? Php
Namespace Common \ Controller;
Use Think \ Controller;
/**
* Frontend and backend public base classes
* Modify author: Jack
* Modify time: 2014-7-12
*/
Class BaseController extends Controller {

Public function _ initialize () {// global variable
Dump ('base class ');
$ This-> cfg ();
}
}

In Home/Controller/ZixunController. class. php

The code is as follows:


<? Php
Namespace Home \ Controller;
Use Common \ Controller \ BaseController;
Class ZixunController extends BaseController {

Public function index (){
$ Result = $ this-> lists ();
Dump ($ result );
}
}

Of course, you can also create your own base class in the front and back ends, such as setting up AdminController in the background. class. php inherits BaseController. class. php. create HomeController on the frontend. class. php inherits BaseController. class. each php module inherits its own base class, so that the project can be clearer and avoid repeated wheel creation, saving a lot of things. However, note that each class must declare a namespace, however, the resources used can be defined in their respective base classes without being written once. For example, if AdminController. class. php inherits BaseController. class. php, you do not need to write use Think \ Controller. you can simply use Common \ Controller \ BaseController.

I hope this article will help you with ThinkPHP framework programming.

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.