5.3. Plugins

5.3. Plugins

5.3.1. Layout Switcher

The Zym_Controller_Plugin_LayoutSwitcher package is a collection of two plugins that allow you to dynamically select a layout for Zend_Layout during the routing process. The first plugin, called Zym_Controller_Plugin_LayoutSwitcher_Module, selects the layout based on the requested module. The second plugin, called Zym_Controller_Plugin_LayoutSwitcher_Router, selects the layout based on the requested route. Both of these plugins fire at preDispatch(), so they work as expected when using the _forward() during the routing process to forward to a different module or route.

Rules can be added to the plugins with the addLayout() method. This method takes two arguments: the layout name and the rule name. The rule name is either the name of the route or the name of the module (depending on which plugin you are configuring). If no matching rule is found during the routing process, it will default to Zend_Layout's layout configuration.

Example 5.8. Using layout switchers

<?php
// Route layout switcher: will swith the active layout to 
// admineLayout.phtml when the route called 'admin' is used.
$routeLayoutSwitcher = new Zym_Controller_Plugin_LayoutSwitcher_Route();
$routeLayoutSwitcher->addLayout('adminLayout', 'admin');
 
// Front Controller
$frontController->registerPlugin($routeLayoutSwitcher);
 
// Module layout switcher: will swith the active layout to 
// blogLayout.phtml when the blog module is requested.
$moduleLayoutSwitcher = new Zym_Controller_Plugin_LayoutSwitcher_Module();
$moduleLayoutSwitcher->addLayout('blogLayout', 'blog');
 
// Front Controller
$frontController->registerPlugin($moduleLayoutSwitcher);

5.3.2. RouteByQuery

This plugin allows you to use routing using HTTP GET query params. It maps http://localhost/?module=test&controller=some&action=foo to http://localhost/test/some/foo.

Example 5.9. Using RouteByQuery

RouteByQuery usage is fairly simple as there is nothing special about it. Simply register it with the FrontController.

<?php
$routeByQuery = new Zym_Controller_Plugin_RouteByQuery();
 
// Register with Zend_Controller_Front
$front->registerPlugin($routeByQuery);

5.3.3. Sid

Zym_Controller_Plugin_Sid is a plugin that allows a session to be set from the url. Traditionally, the session identifier would be specified through a query parameter; however, this plugin allows the sid to be formed like so: /index/index/sid/ASD231sd123.

[Note] Note

Specifying the session identifier in url's is a security risk. Be extra careful of what might happen.