How to call each other between ThinkPHP controllers

不言
Release: 2023-03-30 07:06:02
Original
3228 people have browsed it

This article mainly introduces the method of mutual calling between ThinkPHP controllers. This function is mainly implemented through the A() method, which can effectively improve the reuse rate of code. It is of great practical value. Friends in need can refer to it. Next

The example in this article describes the method of mutual calls between ThinkPHP controllers. Share it with everyone for your reference. The specific implementation method is as follows:

ThinkPHP In the same project, how do the methods of two controllers call each other? ThinkPHP provides an A() through which methods between controllers can call each other so that code can be reused.

There seems to be no official documentation on the use of the A() method. Now let’s use an example to explain how to use the A() method.

There are two controllers, ColumnsAction and NewsAction. ncatlist() is the category list method of ColumnsAction. Now I want to call the ncatlist() method in the controller NewsAction.

The code is as follows:

Copy code 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 {  
      
    // 首页  
    public function index() {  
   $Columns=A("Columns");  
   $Columns->ncatlist();  
}
Copy after login

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

Note: The above code is a code fragment of WBlog3.0 (using the core package of thinkphp3.0), but I checked that the thinkphp3.1 and thinkph3.12 core packages still retain method A.

Related recommendations:

thinkphp3.2 implements methods of calling other modules across controllers

thinkphp project how to customize the WeChat sharing description content

The above is the detailed content of How to call each other between ThinkPHP controllers. For more information, please follow other related articles on the PHP Chinese website!

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