How to set page title for controller actions?

I want to have a different name (in my head) for each controller and action. How to do it from the controller?

+5
source share
5 answers

Your controller

    class SiteController {
        public function actionIndex() {
            $this->pageTitle = 'Home page';
            //...
        }

        //..
    } 

Layout file

    <title><?php echo $this->pageTitle; ?></title>

Perhaps you forgot to add the link to your html?

+9
source

If you want to have a different name in every action

Then just set the value CController.pageTitleinside your action:

class MyController extends CController {
    public function actionIndex() {
        $this->pageTitle = "my title";
        // other code here
    }
}

If you want to split a specific title between several actions

One way is to simply follow the above approach, possibly using a class constant as the page name:

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
    }
}

, , " ". , , filter. :

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
    }
}
+8

init . , pageTitle .

:

public function init()
{
  parent::init();
  $this->pageTitle = "My Page Title";
}
+1

Yon : -

$this->set("title", "Enrollment page");

$ urt cpp, .

.

0

VIEW (index.php, view.php, create.php ..)

$this->setPageTitle('custom page title');
0

All Articles