How to change the presentation of block blocks from a controller in CakePHP 3?

Inside the /default.ctp layouts, you will see something like this on line 39:

<div class="header-title"> <span><?= $this->fetch('title') ?></span> </div> 

fetch assumes this is a view block. I could not find this viewer anywhere.

Currently, it just displays the uppercase plural form of the controller. The point is to say if you are in /users/add , fetch('title'); gives you 'Users'

I want to change it. So I tried the following:

 $this->set('title', 'Login'); 

in the controller action /users/login .

Does not work.

I also tried

 $this->assign('title', 'Login'); 

in the controller action /users/login .

I get this error message:

 Call to undefined method App\Controller\UsersController::assign() 

I read the docs here too

I get

Assigning the contents of blocks is often useful when you want to convert a view variable into a block. For example, you can use the block for the page title and sometimes assign the title as a view variable in the controller :

Emphasis is mine.

This suggests that you can use the internal assign controller. I think I proved this to be wrong.

There may be a typo in the documents. Please advise how I can set the title

+2
source share
3 answers

Here's how to do it thanks to the dakota of the irC # cakephp channel.

Inside UsersController.php :

 $this->set('title', 'Login'); 

Inside src/Template/Layouts/default.ctp

above $this->fetch('title');

records:

 if (isset($title)) { $this->assign('title', $title); } 

The question is, how did cakephp 3 set the default value?

The answer is at https://github.com/cakephp/cakephp/blob/3.0/src/View/View.php#L468

Add an image just in case link rot

enter image description here

Where you can see that the default view path will be displayed

+4
source

Assigning the contents of blocks is often useful if you want to convert a view variable into a block. For example, you can use the block for the page title and sometimes assign the title as a view variable in the controller:

The above does not imply that you should use the assignment in your controller (note the bold). The foregoing suggests that instead of using

 $this->start('title'); echo $title; $this->end() 

you can use

 $this->assign('title', $title); 

and the variable $title should be set from your controller.

If you want to make the right path from your controller, you need to write

 $this->set('title', $title); 

and write from your layout / view file

 echo $title; 
+1
source

Cake 3 (and 2?) Gives you the option to override the View class. This is a good place to call assign ().

Example:

 namespace App\View; use Cake\View\View; class AppView extends View { public function initialize() { $this->assign('the_block_name', "Hello from AppView"); } } 

In Layout / default.ctp:

 <?= $this->fetch('the_block_name') ?> 

If, as in my case, you want the "default" block to be defined, the above solution works fine. You can override the default block in any file of the form (.ctp) as follows:

 $this->start('the_block_name'); echo '<h4>Hello from current view!</h4>'; $this->end(); 
0
source

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


All Articles