Home  >  Article  >  Backend Development  >  thinkphp public output of a variable

thinkphp public output of a variable

WBOY
WBOYOriginal
2016-12-01 01:27:351503browse

I tried to do a small project myself (using the thinkphp framework), and encountered a problem regarding template assignment: each template has a header header , no Problem. But here comes the problem. There is a variable in the header that needs to be output to each template that references it. This variable is generated by a method in the controller:
public function header(){

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

It is impossible for me to write this method once on every template controller. Of course, I cannot put 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");
}

}
It’s all like this, how to output the above variable {$lo_id} to each template?
Or maybe I didn’t express the requirement clearly-that is, each template can output a certain variable

Reply content:

I tried to do a small project myself (using the thinkphp framework), and encountered a problem regarding template assignment: each template has a header header , no Problem. But here comes the problem. There is a variable in the header that needs to be output to each template that references it. This variable is generated by a method in the controller:
public function header(){

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

It is impossible for me to write this method once on every template controller. Of course, I cannot put this method on the parent class controller of the template controller. The template controller:
namespace HomeController;

use ThinkController;

class IndexController extends Controller
{

<code>public function index()
{
$this->display("url");
}
</p>
<p>}<br>It’s all like this, how to output the above variable {$lo_id} to each template? <br>Or maybe I didn’t express the requirement clearly-that is, each template can output a certain variable</p>

                            
                        
            <p class="answer fmt" data-id="1020000007296915">
                                    
</p>
<p>Put the common parts into the parent class and the subclasses can inherit it</p>
<pre class="brush:php;toolbar:false"><code><?php
namespace HomeController;

use ThinkController;

class BaseController extends Controller{
    public function header(){
        $this->display("url");
    }
}
</code>
<code><?php
namespace HomeController;

use ThinkController;
use Home/BaseController

class IndexController extends BaseController{
    public function index(){
        $this->header();
    }
}
</code>

1. Every page will have the $this->assign('data',$data); method.
Just attach this variable, there is no need to write a separate method
2. Write a base class. Inherit Controller, implement this method, and then your page inherits the base class, so you don’t have to write it repeatedly
(The code example is already given on the first floor)

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
Previous article:Apache2.4 and NginxNext article:Apache2.4 and Nginx