set route with Zend_Rest_Route in routes.ini

1.3k Views Asked by At

I am trying to build an API with Zend_Rest_Route. In my tasks module I have 3 controllers :

  • tasksController
  • typesController
  • statusController

I can access my tasks controller by setting this in my routes.init

routes.qtasks.type = "Zend_Rest_Route"
routes.qtasks.route = "/tasks/:id"
routes.qtasks.defaults.module = "tasks"
routes.qtasks.defaults.controller = "tasks"
routes.qtasks.tasks = "tasks"

routes.tasktypes.type = "Zend_Rest_Route"
routes.tasktypes.route = "tasks/types/:id"
routes.tasks.defaults.module = "tasks"
routes.tasktypes.defaults.controller = "types"
routes.tasktypes.tasks = "types"

However, I can access my tasks/types, tasks/types/1, etc but to access my tasks controller, I have to use the url /tasks/tasks/1 even if I have set the route = "/tasks". I am expected to be able to access it via /tasks/1 Why this doesn't work as expected ? (it works perfectly fine when I used to use it with Zend_Controller_Router_Route_Regex).

Update : I have 4 controllers in my module (some REST, some normal) My REST Controllers (which extend Zend_Rest_Controller so it automaticaly redirect to the correct method) have the standard REST methods (indexAction, getAction, putAction, postAction, deleteAction)

  • TasksController (Zend_Rest_Controller)
  • TypesController (Zend_Rest_Controller)
  • StatusController (Zend_Controller_Action) but simulated as a Rest Controller (see routes.ini)
  • ViewController (Zend_Controller_Action) => manage the different php views associated with phtml

The StatusController is a Zend_Action_Controller because Zend does not seem to manage hierarchical REST url (in this case /tasks/types/:type_id/status/:id). I use my checkhttprequest method to forward to the correct action.

Here is my routes.ini for all that controllers :

; task views
routes.tasksindex.type = "Zend_Controller_Router_Route_Regex"
routes.tasksindex.route = "tasks/view"
routes.tasksindex.defaults.controller = "view"
routes.tasksindex.defaults.module = "tasks"
routes.tasksindex.defaults.action = "index"

routes.tasksviews.type = "Zend_Controller_Router_Route_Regex"
routes.tasksviews.route = "tasks/view/(\d+)"
routes.tasksviews.defaults.controller = "view"
routes.tasksviews.defaults.module = "tasks"
routes.tasksviews.defaults.action = "view"
routes.tasksviews.map.1 = "id"

routes.tasksadmin.type = "Zend_Controller_Router_Route_Regex"
routes.tasksadmin.route = "tasks/admin"
routes.tasksadmin.defaults.controller = "view"
routes.tasksadmin.defaults.module = "tasks"
routes.tasksadmin.defaults.action = "admin"

; tasks REST API
routes.tasks.type = "Zend_Rest_Route"
routes.tasks.route = "tasks/:id"
routes.tasks.defaults.module = "tasks"
routes.tasks.defaults.controller = "tasks"
routes.tasks.tasks = "tasks"

; task types REST API
routes.tasktypes.type = "Zend_Rest_Route"
routes.tasktypes.route = "tasks/types/:id"
routes.tasktypes.defaults.module = "tasks"
routes.tasktypes.defaults.controller = "types"
routes.tasktypes.tasks = "types"

; task type status Simulated REST API
routes.taskstypestatus.type = "Zend_Controller_Router_Route_Regex"
routes.taskstypestatus.route = "tasks/types/(\d+)/status/?([0-9]+)?"
routes.taskstypestatus.defaults.controller = "status"
routes.taskstypestatus.defaults.module = "tasks"
routes.taskstypestatus.defaults.action = "checkhttprequest"
routes.taskstypestatus.map.1 = "type_id"
routes.taskstypestatus.map.2 = "id"

Note : /the TypesController.php and StatusController.php work. I only have an issue with the TasksController which I want to be accessible as http://demo.localhost/tasks and not http://demo.localhost/tasks/tasks/

3

There are 3 best solutions below

5
Remko On

Is it possible to replace both blocks of route with something like:

routes.tasks.type = "Zend_Rest_Route"
routes.tasks.route = "/tasks/:controller/:id"
routes.tasks.defaults.controller = tasks
routes.tasks.defaults.module = tasks
routes.tasks.defaults.id =

You set a default for the /:id param to null so if the param is not set the route is still triggered.

2
takeshin On

This line looks suspicious:

routes.qtasks.tasks = "tasks"

Did you mean:

routes.qtasks.action = "tasks"

or:

routes.qtasks.defaults.tasks = "tasks"

You probably need to set the default value for the id param too.

0
Jose Arandi On

It seems like Zend requires the route to be named 'rest', and modules like properties along with their controllers as values. For example:

routes.rest.type = Zend_Rest_Route
routes.rest.tasks = tasks,types

This way, Tasks_TasksController and Tasks_TypesController will be REST APIs.

Check carefully the documentation.