CakePHP - How to set page title to element name?

OK, so I'm trying to teach myself the CakePHP framework, and I'm trying to get a simple demo application for myself for myself.

I have controllers, views, and models, all configured and working, but I want to do something a little more than the basic online help shows.

I have a guitars_controller.php file as follows:

<?php class GuitarsController extends AppController { var $name = 'Guitars'; function index() { $this->set('Guitars', $this->Guitar->findAll()); $this->pageTitle = "All Guitars"; } function view($id = null) { $this->Guitar->id = $id; $this->set('guitar', $this->Guitar->read()); // Want to set the title here. } } ?> 

The Guitar object contains the Name attribute, and I would like to be able to set it as pageTitle for individual page views.

Can someone please indicate how I will do this?

NB . I know that there is a general disagreement with where in the application to set such data, but for me it is connected with the data.

+4
source share
10 answers

These actions are agnostic of the model, so they can be added to the 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 trigger a guitar controller index action that does not exist, so it will be launched in the AppController (which inherits GuitarsController). The same goes for the action of the view. This will also work for your DrumsController, KeyboardsController, etc. Etc.

+5
source

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 already be set in the controller.

For security, you should make sure that your layout / view displaying the Title html page encodes this arbitrary data in order to avoid attack attacks and broken html

 <?php echo h( $title_for_layout ); ?> 
+4
source

In response to your own answer about Fr. This is something 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 the identifier is set and valid, etc., since this is user input URL.

+1
source

As with CakePHP 1.3, the page name change 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

+1
source
 $this->pageTitle = $this->Guitar->Name; 

It should go in the view, although I'm not PHP, or cakePHP, but this is what the view should do, not the controller.

0
source

He must enter the controller. See this

0
source

"but I want to do something a little more than basic online help."

Isn't that always cod? So much documentation is focused on the minimum minimum that it really doesn't help. You can complete many of the available tutorials, but as soon as you take 1 step from the reservation, there is confusion. Well, this is either a minimal or a professional developer, but rarely gets into this sweet spot of lightness, clarity and depth.

I am currently rewriting some Zend Framework documentation for my own use so that I can smooth out inconsistencies, clarify fading assumptions, and understand the essence of "best practice." My mantra: simplicity, clarity, depth. Simplicity, clarity, depth.

0
source

Ah, the non-obvious answer is this:

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

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

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

Thanks to everyone who tried to help.

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

What about

 pr( $this ); 
0
source

OK, I really want to set the page title in the controller instead in the view. So here is 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 cannot set $this->title inside any action, this will not work. It seems to me that the webpage reaches the action after rendering, however you can do it in beforeFilter .

0
source

Source: https://habr.com/ru/post/1277657/


All Articles