简体   繁体   中英

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.

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.

with zend framework default router , your url's didn't work. this links must work:

 http://example.com/companies/add
 http://example.com/companies/edit/id/some-company
 http://example.com/companies/addmachines/id/some-company//
 http://example.com/companies/contacts/mod/edit/companyid/another-company/

format:

http://yoursite.com/Module(if available)/Controller/Action/param1/val1/param2/val2/?param3=val3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM