set route with Zend_Rest_Route in routes.ini - zend-framework

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/

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.

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.

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.

Related

Zend Framework alternate url routing

Not sure if it is possible with ZF 1.11 to do the following
Normal method of displaying a photo
www.mysite.com/photos/display/377486
Where 377486 is the id of a photo within mysql and can be any integer
Is it possible to have the url shortened so I get the same with
www.mysite.com/photos/377486
If this is possible what is this technique/method called?
Many thanks
No, You need 2 different route.
You can test :
routes.photos.type = "Zend_Controller_Router_Route_Regex"
routes.photos.route = "photos/display/(\d+)"
routes.photos.defaults.controller = "photos"
routes.photos.defaults.action = "display"
routes.photos.map.1 = "id"
routes.photos.reverse = "books/%d"
routes.photos_short.type = "Zend_Controller_Router_Route_Regex"
routes.photos_short.route = "photos/(\d+)"
routes.photos_short.defaults.controller = routes.photos.defaults.controller
routes.photos_short.defaults.action = routes.photos.defaults.action
routes.photos_short.map.1 = "id"
routes.photos_short.reverse = "books/%d"

zend route regex issue

I am trying to make a route in the ini file to match the following URLs, but I have been unsuccessful.
/add-announce.html
/add-announce-books-53.html
My route is this:
routes.add_announcement.type = "Zend_Controller_Router_Route_Regex"
routes.add_announcement.route = "/add-announce(-[a-zA-Z_]+)?(-[\d]+)?.html"
routes.add_announcement.defaults.module = announcement
routes.add_announcement.defaults.controller = frontend
routes.add_announcement.defaults.action = add
routes.announcements.defaults.catName = null
routes.announcements.defaults.catId = null
routes.add_announcement.map.catName = 1
routes.add_announcement.map.catId = 2
Maybe because your matches has '-' at the beginning?, Can you try with:
routes.add_announcement.route = "add-announce(?:-([a-zA-Z_]+))?(?:-([\d]+))?.html"
EDIT: I just found the error, you set the mapped values wrong:
routes.add_announcement.map.catName = 1
routes.add_announcement.map.catId = 2
instead you have to do it like this:
routes.add_announcement.map.1 = "catName"
routes.add_announcement.map.2 = "catId"
Also routes.announcements.defaults.catName shouldn't be routes.add_announcement.defaults.catName?

Zend newbie - logic based on URL

url is:
http://mySite.com/adminusers/listusers/
my controller is called
public function listusersAction()
http://mySite.com/adminusers/listusers/regular
http://mySite.com/adminusers/listusers/premium
http://mySite.com/adminusers/listusers/excecutive
How can I capture last piece of the URL as a paremter in controller file listusersAction?
thanks
I usually use regex routes for that kind of thing. For example, you could setup a route with the following definition :
routes.adminusers-listusers.type = "Zend_Controller_Router_Route_Regex"
routes.adminusers-listusers.route = "adminusers/listusers/(\w+)"
routes.adminusers-listusers.defaults.controller = adminusers
routes.adminusers-listusers.defaults.action = listusers
routes.adminusers-listusers.map.1 = "usertype"
routes.adminusers-listusers.reverse = "adminusers/listusers/%s"
Then, on your controller, you can access usertype by querying the request params :
$params = $this->getRequest()->getParams();
$userType = $params['usertype'];

How do I write chains of chains of ... of route in a ini file for the Zend framework?

I am trying to define routes as below with an INI file for the Zend Framework:
http://api.example.com/servicename/{version}/users/:userid/items/:itemid
routes.host.type = "Zend_Controller_Router_Route_Hostname"
routes.host.route = "api.example.com"
routes.host.chains.api.type = "Zend_Controller_Router_Route_Static"
routes.host.chains.api.route = "servicename/v1"
routes.host.chains.api.defaults.controller = "servicename-v1-api"
routes.host.chains.api.defaults.action = "index"
routes.host.chains.api.chains.users.chains.user.type = "Zend_Controller_Router_Static"
routes.host.chains.api.chains.users.route = "users"
routes.host.chains.api.chains.users.defaults.controller = "users"
routes.host.chains.api.chains.users.defaults.action = "index"
routes.host.chains.api.chains.users.chains.user.type = "Zend_Controller_Router_Route"
routes.host.chains.api.chains.users.chains.user.route = ":id"
routes.host.chains.api.chains.users.chains.user.defaults.controller = "user"
routes.host.chains.api.chains.users.chains.user.defaults.action = "index"
...
The host-api route works fine but when I try to reach the other routes, I get the error 'No route matched the request'
The chains.something.chains.somethingelse seems awkward so it probably isn't the correct way to do it. Anyone?
I think I have found how to do it. Basically, you define the parts of each routes with abstract set to true and link them all with routes whose type is set to Zend_Controller_Router_Route_Chain. Something like:
[...]
routes.users.type = "Zend_Controller_Router_Route"
routes.users.route = "users"
routes.users.abstract = "1"
routes.users.defaults.controller = "users"
routes.users.defaults.action = "index"
routes.host-api-users.type = "Zend_Controller_Router_Route_Chain"
routes.host-api-users.chains = "host, api, users"

Adding sub domain based routes in Zend framework

I am newbie to Zend framework, I am using .ini file to add routes in my application.
I have 2 routes for different modules which
resources.router.routes.news_view.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.news_view.route = "([0-9\-]+)/([a-zA-Z0-9\-]+)\.html"
resources.router.routes.news_view.defaults.module = "news"
resources.router.routes.news_view.defaults.controller = "index"
resources.router.routes.news_view.defaults.action = "view"
resources.router.routes.news_view.map.1 = "date"
resources.router.routes.news_view.map.2 = "title"
resources.router.routes.edu_view.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.edu_view.route = "([0-9\-]+)/([a-zA-Z0-9\-]+)\.html"
resources.router.routes.edu_view.defaults.module = "education"
resources.router.routes.edu_view.defaults.controller = "index"
resources.router.routes.edu_view.defaults.action = "article"
resources.router.routes.edu_view.map.1 = "date"
resources.router.routes.edu_view.map.2 = "title"
the url pattern is like
http://news.mysite.com/27-08-09/sample.html
http://education.mysite.com/27-08-09/sample.html
the problem I face is the last defined route is assigned for both the modules.
can anyone suggest a solution for this.
Hi after much browsing in the web I came up with this solution for my problem
resources.router.routes.www.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.www.route = ":module.findchennai.com"
resources.router.routes.www.defaults.module = "www"
resources.router.routes.www.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.www.chains.index.route = ":controller/:action/*"
resources.router.routes.www.chains.index.defaults.controller = "index"
resources.router.routes.www.chains.index.defaults.action = "index"
The above code maps the module with sub domain
resources.router.routes.news.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.news.route = "news.findchennai.com"
resources.router.routes.news.defaults.module = "news"
resources.router.routes.edu.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.edu.route = "education.findchennai.com"
resources.router.routes.edu.defaults.module = "education"
resources.router.routes.edu.chains.list.type = "Zend_Controller_Router_Route"
resources.router.routes.edu.chains.list.route = ":categ/:page"
resources.router.routes.edu.chains.list.defaults.controller = "index"
resources.router.routes.edu.chains.list.defaults.action = "category"
resources.router.routes.edu.chains.list.defaults.page = 1
resources.router.routes.news.chains.list.type = "Zend_Controller_Router_Route"
resources.router.routes.news.chains.list.route = ":categ/:page"
resources.router.routes.news.chains.list.defaults.controller = "index"
resources.router.routes.news.chains.list.defaults.action = "category"
resources.router.routes.news.chains.list.defaults.page = 1
This solves the problem I faced and now could map correctly to the following urls
http://news.mysite.com/27-08-09/sample.html
http://education.mysite.com/27-08-09/sample.html
Still if some one knows how to optimise the above code further, Please let me know.
Do both subdomains call into the same index.php?
If they don't only set the route based on the appropriate sub domain instead of setting both routes in both sub domains.
If they do either read from the $_SERVER['HTTP_HOST'] variable and set the correct route based on the subdomain or set an environment variable in your .htaccess file so you can tell which subdomain you are in.