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.