We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get Last Action name ?

So i need to get the last action name in a other action . Example :

Navigate FROM clients/admin OR clients/member TO clients/create

So in my clients/create i need to know where is it comming from , from the admin or the member view.

Note: $this->dispatcher->getPreviousActionName() is returning NULL

edited Sep '15

getPreviousActionName() only returns the last action if the current was forwarded by the dispatcher. Navigation will generate a new response... so you lose the previous data, just like in any PHP script.

You could determine it from the HTTP_REFERER, but depending on your routing setup it could be cumbersome. Or save the last action in a session variable, but opening your app in multiple tabs could mess it up.

Maybe it was just an example, but whether someone is coming from the admin side or member side should be stored in the session.

A simple solution I use.

class ControllerBase extends Controller
{
    public function afterExecuteRoute($dispatcher)
    {
        $this->session->set('lastUri', $this->router->getRewriteUri());
    }

    protected function getLastUri()
    {
        return $this->session->get('lastUri');
    }
}

or

class ControllerBase extends Controller
{
    public function afterExecuteRoute($dispatcher)
    {
        $this->session->set('lastControllerAction', $dispatcher->getControllerName().'/'.$dispatcher->getActionName());
    }

    protected function getLastControllerAction()
    {
        return $this->session->get('lastControllerAction');
    }
}