Mutual call between ThinkPHP controllers

Source: Internet
Author: User
This article mainly introduces the methods for implementing mutual calls between the ThinkPHP controllers. This function is mainly implemented through the () method, which can effectively improve the code reuse rate and is of great practical value, for more information about ThinkPHP, see ThinkPHP.

This article describes how to implement mutual calls between the ThinkPHP controllers. Share it with you for your reference. The specific implementation method is as follows:

How do two controllers call each other in ThinkPHP's same project? ThinkPHP provides A () that allows the methods between controllers to call each other, so that the code can be reused.

The official website does not seem to have any documentation on the use of the () method. for example, use the () method.

There are two controllers: ColumnsAction and NewsAction. Ncatlist () is the classification list method of ColumnsAction. now I want to call the ncatlist () method in the controller NewsAction.

The code is as follows:

The code is as follows:

Class ColumnsAction extends Action {
Public function ncatlist (){
$ Columns = new Model;

$ News = M ("News ");
$ List = $ Columns-> query ("SELECT concat (colPath, '-', colId) AS bpath, colId, colPid, colPath, colTitle, description, ord FROM ". C ('Db _ prefix '). "columns where typeid = 1

Order by bpath, colId ");

$ This-> assign ('alist', $ list );
}
}
Class NewsAction extends CommonAction {

// Homepage
Public function index (){
$ Columns = A ("Columns ");
$ Columns-> ncatlist ();
}

In this way, you can loop through alist in the template to obtain the category list.

Note: The above code is broken by WBlog3.0 (using the core package of thinkphp3.0). However, I checked thinkphp3.1 and thinkph3.12 core packages and kept the method.

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.