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

后端 未结 10 835
囚心锁ツ
囚心锁ツ 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:40

    It should go in the controller. See this

    0 讨论(0)
  • 2021-01-03 02:41

    As of CakePHP 1.3, setting page title has been changed.

    $this->pageTitle = "Title"; //deprecated
    
    $this->set("title_for_layout",Inflector::humanize($this->name)); // new way of setting title
    

    Note: More about Inflector: http://api13.cakephp.org/class/inflector#method-Inflector

    0 讨论(0)
  • 2021-01-03 02:43

    "but I want to do something slightly more than the basic online help shows."

    Isn't that always the rub? So much documentation is geared towards a bare minimum that it really does not help much. You can complete many of the tutorials available but as soon as you take 1 step off the reservation the confusion sets in. Well, it's either bare minimum or pro developer maximum but rarely hits that sweet spot of ease, clarity and depth.

    I'm currently rewriting some Zend Framework documentation for my own use simply so I can smooth out the inconsistencies, clarify glossed over assumptions and get at the core, "best practice" way of understanding it. My mantra: Ease, clarity, depth. Ease, clarity, depth.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题