This article mainly introduces the method of mutual call between thinkphp controller, mainly through a () method to achieve this function, can effectively improve the code reuse, very practical value, the need for friends can refer to the next
This paper describes the method of mutual invocation between thinkphp controllers. Share to everyone for your reference. The implementation method is as follows:
thinkphp How do the methods of two controllers call each other in the same project? Thinkphp provides a () that allows the methods of the controller to call each other, allowing the code to be reused.
The official seems to have no correlation with the A () method using the documentation, now, for example, using the A () method.
There are two controllers, Columnsaction and newsaction. Ncatlist () is the Columnsaction classification list method, and now I want to call the Ncatlist () method in the controller newsaction.
The code is as follows:
Copy the Code code 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 { //Home public Function index () { $Columns =a ("Columns"); $Columns->ncatlist (); }
This way you can loop alist to get a list of categories in the template.
Description: The code above is WBlog3.0 (using THINKPHP3.0 's core package), but I've looked at the thinkphp3.1 and thinkph3.12 core packages to keep the A method.