Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Controller_Router_Rewrite Class Reference

Specialized version of the rewrite router with a few modified behaviors: More...

List of all members.

Public Member Functions

 addRoute ($name, Zend_Controller_Router_Route_Interface $route, $prepend=false)
 Add route to the route chain.
 assemble ($userParams, $name=null, $reset=false, $encode=true)
 Extended to provide several special behaviors:
 route (Zend_Controller_Request_Abstract $request)
 Specialized to strip an embedded branch name from the request's path info prior to matching on requests.

Detailed Description

Specialized version of the rewrite router with a few modified behaviors:

Knowledge of the convention for embedding a particular site branch name in the request. Strips the branch name from the request path info prior to matching/routing urls and injects it prior to assembling them.

Overrides the Zend_Controller_Router_Rewrite's assemble method so that when an explicit router name is not provided, the 'default' route is used. Normally, once a named route is used, it will be used for subsequent URL generation unless another explicitly named route is specified. This means that when the default route is desired for URL generation, it would have had to be explicitly set, which is undesired.

Also, always resets url parameters (except for module, controller and action) to avoid bugs that tend to occur when the current request parameters bleed into the next request.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4Cms_Controller_Router_Rewrite::addRoute ( name,
Zend_Controller_Router_Route_Interface $  route,
prepend = false 
)

Add route to the route chain.

Extended to support prepending the route. The last route wins, prepending gives the route lower priority.

If route contains method setRequest(), it is initialized with a request object

Parameters:
string$nameName of the route
Zend_Controller_Router_Route_Interface$routeInstance of the route
bool$prependoptional - defaults to false prepend lowers the priority
Returns:
Zend_Controller_Router_Rewrite
    {
        parent::addRoute($name, $route);

        // if prepend is specified, move route to the beginning of the array.
        if ($prepend) {
            $this->_routes = array($name => $route) + $this->_routes;
        }

        return $this;
    }
P4Cms_Controller_Router_Rewrite::assemble ( userParams,
name = null,
reset = false,
encode = true 
)

Extended to provide several special behaviors:

  • Injects the branch name into the assembled url if present.
  • Overrides the parent assemble such that when a specific name is not provided, the default route is used.
  • Also, always resets url parameters (except for module, controller and action).
Parameters:
array$userParamsOptions passed by a user used to override parameters
mixed$nameThe name of a Route to use
bool$resetReset ALL url parameters including module, controller, action.
bool$encodeTells to encode URL parts on output
Returns:
string Resulting absolute URL path
Exceptions:
Zend_Controller_Router_Exception
    {
        if ($name == null) {
            $name = 'default';
        }

        // merge global module, controller and action params into
        // user params unless reset is explicitly true.
        if ($reset !== true) {
            $globalParams = array_intersect_key(
                array_flip(array('module', 'controller', 'action')),
                $this->_globalParams
            );
            $userParams = array_merge($globalParams, $userParams);
        }

        // if request has no branch, parent can take it from here.
        $request = $this->getFrontController()->getRequest();
        if (!$request instanceof P4Cms_Controller_Request_Http || !$request->getBranchName()) {
            return parent::assemble($userParams, $name, true, $encode);
        }

        // request contains an embedded branch name, we want to inject it into the
        // assembled url - the easiest way to do this is to modify the base url.
        $baseUrl = $request->getBaseUrl();
        $request->setBaseUrl($request->getBranchBaseUrl());

        $url = parent::assemble($userParams, $name, true, $encode);

        // restore original base url.
        $request->setBaseUrl($baseUrl);

        return $url;
    }
P4Cms_Controller_Router_Rewrite::route ( Zend_Controller_Request_Abstract $  request)

Specialized to strip an embedded branch name from the request's path info prior to matching on requests.

Parameters:
Zend_Controller_Request_Abstract$requestthe request to route
Returns:
Zend_Controller_Request_Abstract the request with route params set.
    {
        // if request does not contain an embedded branch name, let parent route it as-is.
        if (!$request instanceof P4Cms_Controller_Request_Http || !$request->getBranchName()) {
            return parent::route($request);
        }

        // request contains an embedded branch name, we need to strip it
        // from the path before the parent router can match against it.
        $path = $request->getPathInfo();
        $request->setPathInfo($request->getBranchlessPath());

        // let parent route the request now that we have stripped the branch
        $request = parent::route($request);

        // restore original path.
        $request->setPathInfo($path);

        return $request;
    }

The documentation for this class was generated from the following file: