ThinkPHP控制器间实现相互调用的方法,thinkphp控制器
本文实例讲述了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框架程序设计有所帮助。
直接 $this -> function(); 就可以了... 完全把Action当做一个Class看待即可,灵活使用,参数也是可以加的,不过声明的时候尽量加上默认参数,免得通过模块调用的时候,报出无参数的提醒错误。
将获取ID的用另外的方法封装,比如protected function getId(){
$id = //获取方法
return $id
}
其他方法中进行调用$id = $this->getId();

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
