ThinkPHP controllers call each other. thinkphp controller _ PHP Tutorial

Source: Internet
Author: User
The ThinkPHP controller calls each other. ThinkPHP controllers call each other. This document describes how thinkphp controllers call each other. Share it with you for your reference. ThinkPHP controller

This article describes how to implement mutual calls between the ThinkPHP controllers. Share it with you for your reference. The specific implementation method is as follows:

How do two controllers call each other in ThinkPHP's same project? ThinkPHP provides A () that allows the methods between controllers to call each other, so that the code can be reused.

The official website does not seem to have any documentation on the use of the () method. for example, use the () method.

There are two controllers: ColumnsAction and NewsAction. Ncatlist () is the classification list method of ColumnsAction. now I want to call the ncatlist () method in the controller NewsAction.

The code is as follows:

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 {

// Homepage
Public function index (){
$ Columns = A ("Columns ");
$ Columns-> ncatlist ();
}

In this way, you can loop through alist in the template to obtain the category list.

Note: The above code is broken by WBlog3.0 (using the core package of thinkphp3.0). However, I checked thinkphp3.1 and thinkph3.12 core packages and kept the method.

I hope this article will help you with ThinkPHP framework programming.


In thinkphp, how does one call another method of this module in the method of one module? Can the thinkphp method pass parameters?

Just $ this-> function... simply treat Action as a Class and use it flexibly. parameters can also be added. However, if you declare an Action, add the default parameter as much as possible to avoid calling through the module, A parameter-free reminder error is returned.
 

Value of the current operation method of thinkphp to call other operation methods

Encapsulate the retrieved ID in another method, such as protected function getId (){
$ Id = // Obtain method
Return $ id
}
Call $ id = $ this-> getId () in other methods ();

Examples in this article describes how to implement mutual calls between the ThinkPHP controllers. Share it with you for your reference. Specific implementation...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.