0
votes

I'm building a Zend Framework 1.11.11 application and would like to make the routes and content database driven.

I've written a FrontController Plugin that retrieves the 'paths' from the database and creates an entry in the Router for each one, with the associated controller and action.

However, I'd like to be able to use 'aliases' - a URL that behaves like a normal URL, but is an alias.

For example, if I create the following:

// Create the Zend Route
$entry = new Zend_Controller_Router_Route_Static(
    $route->getUrl(), // The string/url to match
    array('controller'  => $route->getControllers()->getName(),
            'action'    => $route->getActions()->getName())
);                    
// Add the route to the router
$router->addRoute($route->getUrl(), $entry); 

Then a route for /about/ for example can goto the staticController, indexAction.

However, what's the best way for me to create an alias of this route? So if I went to /abt/ it would render the same Controller and Action?

To me it doesn't make sense to recreate the same route as I'll be using the route as the page 'identifier' to then load content from the database for the page...

1

1 Answers

1
votes

you can extend static router:

class My_Route_ArrayStatic extends Zend_Controller_Router_Route_Static
{
    protected $_routes = array();

    /**
     * Prepares the array of routes for mapping
     * first route in array will become primary, all others
     * aliases
     * 
     * @param array $routes array of routes
     * @param array $defaults 
     */
    public function __construct(array $routes, $defaults = array())
    {
        $this->_routes = $routes;
        $route = reset($routes);
        parent::__construct($route, $defaults);
    }

    /**
     * Matches a user submitted path with a previously specified array of routes
     * 
     * @param string $path
     * @param boolean $partial
     * @return array|false 
     */
    public function match($path, $partial = false)
    {
        $return = false;

        foreach ($this->_routes as $route) {
            $this->setRoute($route);
            $success = parent::match($path, $partial);
            if (false !== $success) {
                $return = $success;
                break;
            }
        }

        $this->setRoute(reset($this->_routes));
        return $return;
    }

    public function setRoute($route)
    {
        $this->_route = trim($route, '/');
    }
}

and add new router this way:

$r = My_Route_ArrayStatic(array('about', 'abt'), $defaults);