Check if an action was called from within a Controller
Posted by Felix Geisendörfer, on Jul 06, 2006 - in PHP & CakePHP » Controllers, Components & Shells
Gopher just asked me weather it was possible in CakePHP to see if an action was invoked by the dispatcher or called by $this->action();. So since I think this is one of these things that could be useful for others as well, here is my solution:
All you need to do is to check whether $this->action and the name of the function that is currently beeing executed match. This can be done like this:
php
-
if ($this->action == __FUNCTION__)
So for example you could use it like this:
php
-
class PostsController extends AppController
-
{
-
function index()
-
{
-
$post3 = $this->view(3);
-
}
-
-
function view($id)
-
{
-
-
if ($this->action != __FUNCTION__)
-
return $post;
-
else
-
$this->render();
-
}
-
}
Ok, this is a pretty useless example I have to admit, but I'm sure there are more interesting ways to utilize this ; ).
--Felix Geisendörfer aka the_undefined