Home  >  Article  >  Backend Development  >  How to implement mutual calls between ThinkPHP controllers, thinkphp controller_PHP tutorial

How to implement mutual calls between ThinkPHP controllers, thinkphp controller_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:381059browse

How to implement mutual calls between ThinkPHP controllers, thinkphp controller

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

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


In this way, you can loop through 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.

I hope this article will be helpful to everyone’s ThinkPHP framework programming.

How to call another method of a module within a method in thinkphp? Can thinkphp methods pass parameters?

Just $this -> function(); and that's it... Just treat Action as a Class and use it flexibly. Parameters can also be added, but try to add default parameters when declaring to avoid passing When the module is called, a parameterless reminder error is reported.


thinkphp’s current operation method calls the value of other operation methods

Encapsulate the ID acquisition with another method, such as protected function getId(){
$id = //Get method
return $id

}
Call in other methods $id = $this ->getId();



http://www.bkjia.com/PHPjc/904010.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904010.htmlTechArticleHow to implement mutual calls between ThinkPHP controllers, thinkphp controller This article describes the implementation of mutual calls between ThinkPHP controllers method. Share it with everyone for your reference. Specific implementation...
Statement:
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