Php 如何设置控制器操作的页面标题?
我想有不同的标题(在头部)为每个控制器和行动。Php 如何设置控制器操作的页面标题?,php,controller,yii,Php,Controller,Yii,我想有不同的标题(在头部)为每个控制器和行动。 如何从控制器执行此操作?您可以使用函数init或before action,或在实际操作调用之前运行哪个调用。因此,在该函数中,可以为控制器设置publicpagetitle变量 这样使用: public function init() { parent::init(); $this->pageTitle = "My Page Title"; } 你可以这样给予:- $this->set("title", "Enrollmen
如何从控制器执行此操作?您可以使用函数init或before action,或在实际操作调用之前运行哪个调用。因此,在该函数中,可以为控制器设置publicpagetitle变量 这样使用:
public function init()
{
parent::init();
$this->pageTitle = "My Page Title";
}
你可以这样给予:-
$this->set("title", "Enrollment page");
并在您的ctp文件中使用此$title,方法是指定不同的名称或标题
试试这个..你的控制器
布局文件
如果您想在每个操作中使用不同的标题,可能忘记在html中添加引用了?
然后只需在您的操作中设置值:
class MyController extends CController {
public function actionIndex() {
$this->pageTitle = "my title";
// other code here
}
}
如果要在多个操作之间共享特定标题
一种方法是简单地遵循上述方法,可能使用类常量作为页面标题:
class MyController extends CController {
const SHARED_TITLE = "my title";
public function actionIndex() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
public function actionFoo() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
}
但是,这要求您在希望将每个操作从“标题共享”方案中包括或排除时,都要单独访问每个操作。一个没有这个缺点的解决方案是使用。例如:
class MyController extends CController {
public function filters() {
// set the title when running methods index and foo
return array('setPageTitle + index, foo');
// alternatively: set the title when running any method except foo
return array('setPageTitle - foo');
}
public function filterSetPageTitle($filterChain) {
$filterChain->controller->pageTitle = "my title";
$filterChain->run();
}
public function actionIndex() {
// $this->pageTitle is now set automatically!
}
public function actionFoo() {
// $this->pageTitle is now set automatically!
}
}
如果要在所有操作中使用相同的标题
这是显而易见的,但我提到它是为了完整:
class MyController extends CController {
public $pageTitle = "my title";
public function actionIndex() {
// $this->pageTitle is already set
}
public function actionFoo() {
// $this->pageTitle is already set
}
}
在视图页面中(index.php、VIEW.php、create.php等)
不知何故,添加$this->pageTitle='newTitle'或$this->setPageTitle('newTitle');不会改变标题。还有其他方法吗?我已经测试了我的代码。我认为您正在覆盖任何视图或布局文件中的pageTitle变量。请检查它。我不知道为什么,但是$this->pageTitle不起作用..你说的ctp文件是什么意思?+1一个小提示,确保你在父控制器类中定义了$pageTitle变量,我认为它默认在那里,但检查它不会有什么坏处。但是你需要用这种方式在控制器的每个动作中写相同的行,这不是更好的解决方案。试试我的方法,我希望这是最好的方法。@onkarjanwa你说得对,它需要写在每一页上,但我通常写每个标题。但是默认的pageTitle是在我的扩展类中写的,类控制器在beforeAction中扩展了CController,作为$this->pageTitle='我的默认标题';。也许init也是个好地方,我不知道。@briiC.lv是的,你可以使用我建议的init函数来扩展类控制器,它会覆盖$this->pageTitle变量。
class MyController extends CController {
public function filters() {
// set the title when running methods index and foo
return array('setPageTitle + index, foo');
// alternatively: set the title when running any method except foo
return array('setPageTitle - foo');
}
public function filterSetPageTitle($filterChain) {
$filterChain->controller->pageTitle = "my title";
$filterChain->run();
}
public function actionIndex() {
// $this->pageTitle is now set automatically!
}
public function actionFoo() {
// $this->pageTitle is now set automatically!
}
}
class MyController extends CController {
public $pageTitle = "my title";
public function actionIndex() {
// $this->pageTitle is already set
}
public function actionFoo() {
// $this->pageTitle is already set
}
}
$this->setPageTitle('custom page title');