Method of mutual call between ThinkPHP controllers

  • 2021-07-24 10:30:57
  • OfStack

In this paper, the method of mutual call between ThinkPHP controllers is described by examples. Share it for your reference. The specific implementation method is as follows:

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

There seems to be no official documentation for the use of the A () method, so let's take an example of using the A () method.

There are two controllers, ColumnsAction and NewsAction. ncatlist () is the categorized list method of ColumnsAction, and now I'm going to call the ncatlist () method in the controller NewsAction.

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 { 
     
    // Home page  
    public function index() { 
   $Columns=A("Columns"); 
   $Columns->ncatlist(); 
}

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

Note: The above code is a code version of WBlog 3.0 (using the core package of thinkphp 3.0), but I looked at the core packages of thinkphp 3.1 and thinkph 3.12 that still retain the A method.

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "smarty Template Introduction Basic Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: