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

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

    These actions are model agnostic so can be put in your app/app_controller.php file

    <?php
    class AppController extends Controller {
        function index() {
            $this->set(Inflector::variable($this->name), $this->{$this->modelClass}->findAll());
            $this->pageTitle = 'All '.Inflector::humanize($this->name);
        }
        function view($id = null) {
            $data = $this->{$this->modelClass}->findById($id);
            $this->set(Inflector::variable($this->modelClass), $data);
            $this->pageTitle = $data[$this->modelClass][$this->{$this->modelClass}->displayField];
        }
    }
    ?>
    

    Pointing your browser to /guitars will invoke your guitars controller index action, which doesn't exist so the one in AppController (which GuitarsController inherits from) will be run. Same for the view action. This will also work for your DrumsController, KeyboardsController etc etc.

    0 讨论(0)
  • 2021-01-03 02:20
    $this->pageTitle = $this->Guitar->Name;
    

    It should go in the View though, I don't PHP, or cakePHP, but thats something a view should do, not the controller.

    0 讨论(0)
  • 2021-01-03 02:22
    echo "<pre>"; print_r($this);echo "</pre>";
    

    how about

    pr( $this );
    
    0 讨论(0)
  • 2021-01-03 02:31

    Ah, the none-obvious answer is as follows...

    $this->pageTitle = $this->viewVars['guitar']['Guitar']['Name'];
    

    I found this by placing the following code in the controller (was a long shot that paid off, to be honest)

    echo "<pre>"; print_r($this);echo "</pre>";
    

    Thanks to all those that tried to help.

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

    In response to your own answer about oo paradigm. Its like this :

    function view($id) {
        $this->Guitar->id = $id;
        $this->Guitar->read();
        $this->pageTitle = $this->Guitar->data['Guitar']['name'];
        $this->set('data', $this->Guitar->data);
    }
    

    By the way, you should check if id is set and valid etc, since this is url user input.

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

    You can set this in the controller:

    function view($id = null) {
        $guitar = $this->Guitar->read(null, $id);
        $this->set('guitar', $guitar);
        $this->pageTitle = $guitar['Guitar']['name'];
    }
    

    Or in the view:

    <? $this->pageTitle = $guitar['Guitar']['name']; ?>
    

    The value set in the view will override any value that may have already been set in the controller.

    For security, you must ensure that your layout / view that displays the pageTitle html-encodes this arbitrary data to avoid injection attacks and broken html

    <?php echo h( $title_for_layout ); ?>
    
    0 讨论(0)
提交回复
热议问题