phpzend-frameworkzend-framework-mvc

Nesting controllers in Zend Framework


I'm building a Zend Framework application and created some controllers which correspond to database tables and hold methods for performing CRUD operations on those tables.

I've just started working on the Companies controller which holds methods for adding, editing and deleting companies. For this we can use the traditional controller/action URL pattern:

http://example.com/companies/add
http://example.com/companies/edit/some-company

The problem is that for each Company we also need to manage its Contacts and Machines:

http://example.com/companies/some-company/machines/add
http://example.com/companies/another-company/contacts/edit/some-contact

I just can't get my head around how to manage this in Zend Framework. Should I 'nest' the controllers through routing, or should I make use of modules? Any help would be very much appreciated.


Solution

  • You could easily write custom routes to handle this. For example, this:

    http://example.com/companies/some-company/machines/add
    

    would become in your .ini file:

    routes.machine.route = "companies/:companyname/machines/:action"
    routes.machine.defaults.controller = machines
    routes.machine.defaults.action = index 
    

    This will point the url to the machines controller and sets companyname as a GET-var.

    You can easily change this to suit any form you like.