0
You can pass variables originating from Appcontroller’s callback through the method set
for Cakephp layout Elements?
0
You can pass variables originating from Appcontroller’s callback through the method set
for Cakephp layout Elements?
1
Researching a little more, I found a way to solve:
The function requestAction()
allows the views
and even elements
cakephp request information from any controller/action
that you define, in true MVC style.
Simply declare an action on Controller
specific:
class PostsController extends AppController {
// ...
public function index() {
$posts = $this->paginate();
if ($this->request->is('requested')) { //Se for requisição de outra view/element:
return $posts;
} else { //Senão envia para a view padrão
$this->set('posts', $posts);
}
}
}
Then you call the function within another view or in this case, an element:
helpbox.ctp
<h2>Últimos posts</h2>
<?php $posts = $this->requestAction('posts/index'); ?> <!--Passa a Action como parâmetro -->
<?php foreach ($posts as $post): ?>
<ol>
<li><?php echo $post['Post']['title']; ?></li>
</ol>
<?php endforeach; ?>
Bonus: Caching the element to decrease requests in the Bank:
By calling the element
within some view
or in the layout
, use the argument cache
:
echo $this->element('helpbox', array(), array('cache' => true));
So I can call anyone action
declared as public
in any controller
, of any view
, without the need to pollute my AppController
.
References: http://book.cakephp.org/2.0/pt/views.html#passing-variables-in-a-element
0
But I have seen several users comment that it is not a good practice to use "requestAction", and even it will be discontinued in the cake version 3, but if you just want to set a global variable that can be accessed in other areas, could do so:
class AppController extends Controller
{
function beforeFilter()
{
$visivelElements = ClassRegistry::init('Configuration')->find('all');
$this->set('visivelElements', $visivelElements);
}
}
If it was only visible on a given controller only do one check, something like:
class AppController extends Controller
{
function beforeFilter()
{
if ($this->name == 'Home') {
$visivelElements = ClassRegistry::init('Configuration')->find('all');
$this->set('visivelElements', $visivelElements);
}
}
}
I saw some commenting on this, but I saw Mark Story himself go back after testing in detail.
Browser other questions tagged cakephp
You are not signed in. Login or sign up in order to post.
yes yes, in the same way that you would pass a variable of any other controller to the viwe and Elements. The difference is that a variable of the
AppController
is available to all views and Elements of the system.– Erlon Charles
If I create a method within appController and call it through the callback beforeRender it will try to display a view?
– Marcelo Aymone
I found out how, there is a method called Requestaction that can request information from any control and also pass parameters.. http://book.cakephp.org/2.0/pt/views.html#passing-variables-in-a-element
– Marcelo Aymone
There’s that too, so finish post your answer
– Erlon Charles