routesmodulezend-frameworkcontrollerframeworks

Zend Framework Controller to Module routing


I have been doing my best trying to absorb as much as I can about the Zend Framework which is pretty new to me, I'm about two weeks in since my first attempts and being doing well so far.

However I have run into a small issue I just can't figure out.

So here's the deal, I have a standard Zend Framework project with a number of modules in it. Like this:

- Project
- - Application
- - - configs
- - - controllers
- - - - IndexController
- - - - WMSController
- - - forms
- - - layouts
- - - models
- - - modules
- - - - content
- - - - - controllers
- - - - - - IndexController
- - - - - models
- - - - - views
- - - - - Bootstrap
- - - views
- - - bootstrap
- - public

etc..

Now I have a simple test link set in the view for the WMSController which is supposed to direct someone clicking it to the IndexController of the content module.

<a href="wms/content"> test link to content module</a>

Now as you can probably see this link is NOT going to work since it is pointing towards localhost/wms/content which would be the content action in the WMSController which doesn't exist at this moment.

Now what I want to do is to make the wms/content actually point towards the IndexController of the content module. Why? Well I simply do not want a user to type localhost/content to get to the content module which is part of the WMS. I want to force them to get through the WMS controller first.

I have read numerous things about routing being the solution using the bootstrap or the autoloader but pretty much all of them simply help you with pointing a certain URL to a specific action in a specific controller.

What I want is a URL to point to a specific action in a specific controller in a specific module.

NOTE: All the other controllers and views are still the default generated versions with no changes made to them yet!

If anyone could show me a code example of how this is done it would be much appreciated!


Solution

  • If I understand correctly you simply want /wms/content to point to module content controller Index action index. This is a case where a simple router will work(at least until you want to get more complex)

    In your application.ini add these lines:

    resources.router.routes.content.route = "wms/content"
    resources.router.routes.content.defaults.module = "content" 
    resources.router.routes.content.defaults.controller = "index"
    resources.router.routes.content.defaults.action = "index"
    

    this is the easiest way to build a custom route.
    *Note:*The 4th parameter from the left is the name of the route, so if you use it with a method that allows using route names (the url() helper for example) you can just use the route name.
    $this->url(array(), 'content');

    This is how you would use a named route, personally I would probably consider renaming my module wms (if possible and practical) and just redirecting there from the default Index controller. Because once you start with named and custom routes it's hard to quit...