Wow, that's pretty wild (card) Hector! I haven't had a reason to do anything 
beyond a default front controller. Not sure if it would be more resource 
efficient, but you can simply set useDefaultControllerAlways = true; and loop 
all non existant controllers to your default action. I haven't inspected the 
sequential params you can read from the default, but I would think they're all 
in the _request object.

-----Original Message-----
From: Hector Virgen [mailto:[email protected]] 
Sent: Wednesday, January 25, 2012 12:56 PM
To: Adam Dear
Cc: [email protected]
Subject: Re: [fw-general] Best way to setup a Default controller that will be 
called if controller not found

On Wed, Jan 25, 2012 at 7:20 AM, Adam Dear <[email protected]> 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/

--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to