Make an empty view in cakephp

I need to prevent the visualization of the view in the specified case, but I cannot figure out how to prevent its visualization.

I tried

$this->autoRender=false 

but nothing happened, probably because I use an API engine that controls rendering differently from regular controllers. Does anyone know of any trick to do this?

+6
source share
8 answers

Using $this->layout = 'ajax' seems insufficient.

But using these two lines works:

 $this->layout = 'ajax'; $this->render(false); 
+11
source

Try using the ajax layout $this->layout = 'ajax' , this is the default empty pool that is used for ajax methods.

+4
source

While searching for a solution, I found this answer. Now, using CakePHP 2.4.x, you can use the following code in your controller:

 $this->layout = false; 

This will result in a simple visualization without a layout.

+3
source

This is an old question. The current version of the cake is 3.x, and there is an easy way to use an empty layout.

Add only a controller:

 $this->viewBuilder()->autoLayout(false); 
+2
source
 public function function_without_layout(){ $this->viewBuilder()->autoLayout(false); echo "hello Brij"; exit; } 

$this->layout = false; deprecated in CakePHP version 3.
Use $this->viewBuilder()->autoLayout(false); for CakePHP version 3.

+2
source

Add this to your controller:

 $this->autoRender = false; 

This works in my project.

+2
source

without knowing anything about the API you are using, perhaps try to make an empty layout with empty content and call it in the controller as $this->layout = 'empty_layout'

0
source

The CakePHP 3 autoLayout(false) method from another answer will still try to find the appropriate view / template file for the action you are invoking. Since I don't need any output at all, this did not work for me, so I also needed to create an empty template.

Creating an empty .ctp file for each empty action that you might need is not an option because you usually want to use and reuse it. CakePHP 2 has the $this->viewPath , which allows you to configure the controller to search in the app/View folder, but the CakePHP 3 alternative still looks at the corresponding controller and prefix folders. There is a less obvious way to get CakePHP3 to look for a pattern in the root view.

  • Create src/Template/my_blank_view.ctp
  • Add the following actions to your controller:

     $this->viewBuilder()->layout(false); $this->viewBuilder()->templatePath('.'); // this $this->viewBuilder()->template('my_blank_view'); 

In addition, I use $this->viewBuilder()->layout(false) instead of autoLayout(false) , because the latter type implies that there may be another layout later, where layout(false) just explicitly sets that the layout is not required .

0
source

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


All Articles