search
Homephp教程PHP开发ThinkPHP控制器间如何相互调用方法?

ThinkPHP同一个项目里,两个控制器的方法如何相互调用呢,ThinkPHP提供了一个A(),通过它可以使控制器之间的方法相互调用,使得代码可以重复利用。


官方似乎对A()方法没有相关使用文档,现在通过一个例子来说一下如使用A()方法。


   有两个控制器,ColumnsAction和NewsAction。ncatlist()是ColumnsAction的分类列表方法,现在我要在控制器NewsAction中调用ncatlist()方法。


看代码:

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();  
}

   这样在模板中你就可以循环alist来获取分类列表了。
   说明:上面的代码是WBlog3.0(使用thinkphp3.0的核心包)的代码版断,不过我查看了thinkphp3.1和thinkph3.12核心包还保留着A方法。

       

   

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools