On Wed, Jan 25, 2012 at 7:20 AM, Adam Dear <addrum...@gmail.com> wrote:

> Is the proper way to create a custom route that routes every request
> to my default controller, and then define routes that send requests to
> few actual controllers that will exist or is there a mechanism that I
> could use in a front controller plugin that would be more appropriate?
>
>
I prefer the idea of creating a custom route and mapping it to a common
controller/action that can load any arbitrary page that exists in the
database. For example, you can add this route to your config:

resources.router.routes.page.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.page.route = "(.*)"
resources.router.routes.page.defaults.controller = "page"
resources.router.routes.page.defaults.action = "view"
resources.router.routes.page.map.1 = "slug"
resources.router.routes.page.reverse = "%s"

This would catch *all* requests and route them to the "page" controller's
"view" action. Within that action, you can look in to the database for the
page that matches the "slug" parameter:

$slug = $this->_request->getParam('slug');

If you visit /about, then $slug will be "about". If you visit
/foo/bar/derp.html, then $slug will be "foo/bar/derp.html".

This means you'd have one controller, one action, and not need to
copy/paste the same code in multiple controllers. You can also create new
pages by just adding a new row to the database.

In the case where a page cannot be loaded from the database, throwing an
exception from within the action would trigger your error page (error
controller, error action).

The drawback to this approach is that you'll lose the built-in default
routing that maps the URL to module, controller, and action. You may want
to set up new routes to handle specific controllers. For example, if you
wanted to expose the other action methods in your "page" controller, you
could add a route like this:

resources.router.routes.page.type = "Zend_Controller_Router_Route"
resources.router.routes.page.route = "page/:action/*"
resources.router.routes.page.defaults.controller = "page"

You would have to repeat this for each controller you want to expose.

I hope this helps!

--
*Hector Virgen*
http://www.virgentech.com
Circle me on Google+ https://plus.google.com/101544567700763078999/

Reply via email to