CakePHP - How do i set the page title to an item name?

后端 未结 10 932
囚心锁ツ
囚心锁ツ 2021-01-03 02:13

OK, so I\'m trying to teach myself the CakePHP framework, and I\'m trying to knock up a simple demo app for myself.

I have the controllers, views and models all set

10条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 02:46

    OK, I really want to set the page title in the controller instead in the view. So here's what I did:

    class CustomersController extends AppController {
    
        var $name = 'Customers';
    
        function beforeFilter() {
            parent::beforeFilter();
            $this->set('menu',$this->name);
            switch ($this->action) {
              case 'index':
                $this->title = 'List Customer';
                break;
              case 'view':
                $this->title = 'View Customer';
                break;
              case 'edit':
                $this->title = 'Edit Customer';
                break;
              case 'add':
                $this->title = 'Add New Customer';
                break;
              default:
                $title = 'Welcome to '.$name;
                break;
            }
            $this->set('title',$this->title);
          }
    

    The trick is that you can't set $this->title inside any action, it won't work. It seems to me that the web page reaches action after rendering, however you can do it in beforeFilter.

提交回复
热议问题