这篇文章主要介绍了ThinkPHP控制器间实现相互调用的方法,主要通过A()方法实现这一功能,可以有效的提高代码的重复利用率,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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方法。
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor