Thinkphp public outputs a variable

Source: Internet
Author: User
I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header & amp; lt; includefile & quot; publicheader & quot; & amp; gt;, no problem. but the problem is that there is a variable in the header that needs to be output to each cited... I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header. There is no problem. but the problem is that there is a variable in the header that needs to be output to every template referenced to it. this variable is generated by a method in the controller:
Public function header (){

    $value=$name;    $this->assign('lo_id',$value);}

I cannot write this method once on the controller of each template. of course, I cannot place this method on the parent class controller of the template controller. the template controller:
Namespace HomeController;

Use ThinkController;

Class IndexController extends Controller
{

public function index(){$this->display("url");}

}
How can I output the above variable {$ lo_id} to each template?
Or I may not express the requirement clearly-that is, each template can output a variable.

Reply content:

I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header. There is no problem. but the problem is that there is a variable in the header that needs to be output to every template referenced to it. this variable is generated by a method in the controller:
Public function header (){

    $value=$name;    $this->assign('lo_id',$value);}

I cannot write this method once on the controller of each template. of course, I cannot place this method on the parent class controller of the template controller. the template controller:
Namespace HomeController;

Use ThinkController;

Class IndexController extends Controller
{

public function index(){$this->display("url");}

}
How can I output the above variable {$ lo_id} to each template?
Or I may not express the requirement clearly-that is, each template can output a variable.

Put the public part in the parent class, and inherit the subclass.


  display("url");    }}

  header();    }}

1. every page will have $ this-> assign ('data', $ data); method.
Just attach this variable. you don't need to write a method separately.
2. write a base class. Inherit Controller, implement this method, and then your page will inherit the base class, so you don't need to write it again
(Sample Code already provided on the first floor)

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.