Detect action/view/module and matched route in agavi

For each executed Module/Action(+View) there is an ExecutionContainer (AgaviExecutionContainer). So if you want to retrieve the current executed action, module or view, best is to do

$action = $this->getContext()->getActionName();
$view = $this->getContext()->getViewName();
$module = $container->getModuleName();

But what if you want to retrieve the "real" action, the one and only for the request? That's a little bit more difficult, so here a short introduction how to deal with that.

Whenever the routing of agavi does the work for you, by turning the input data and webbrowser call of a user, into a action and validated data, it triggers an action, executes the view for the outputtype and fills the template. But Slots can be used from the view and trigger then again validation+action+view for their parameters.

This is why the real request route can not be found in the execution container and must be found somewhere else. In the Request.

$route_names = $this->getContext()->getRequest()->getAttribute(
                    'matched_routes', 'org.agavi.routing'

The route_names is nothing but an array, for instance:

    // because our current route is named "product.edit"
    // in routing.xml

This information does help us indirectly to determine the action, which got called! Since the WebRouting has also a getRoute-method, which expects just a route_name, we can retrieve the info now easily:

// we want to have just the first one,
// thatswhy list($first) = $array style
[strong]list($route_name)[/strong] = $this->getContext()->getRequest()->getAttribute(
    'matched_routes', 'org.agavi.routing'

$route = $this->getContext()->getRouting()->getRoute($route_name);

The new $route-Variable now holds an array of all information about the route. In our case, just some are interesting.

$module = $route['opt']['module'];
$action = $route['opt']['action'];

One really should use that

only in case of a default slot, which is used directly in the output_types.xml or used on serveral different pages and has to determine on its own and by using module+action where it's actually embedded.

In agavi, open source, php by
@ 05 Mar 2009, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files