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
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>
Copy after login
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>
Copy after login
<code><?php
namespace HomeController;
use ThinkController;
use Home/BaseController
class IndexController extends BaseController{
public function index(){
$this->header();
}
}
</code>
Copy after login
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)