Zend Framework, mapping the extension of a URL to the format parameter? - zend-framework

It is possible to map the extension of a URL, to the format parameter in ZF?
I'd like the default routing to still work, including mapping parameters from the URI, so you'd be able to say:
http://example.com/controller/action/param1/value1/param2/value2.json
Here: $this->_getParam('format') => "json"
And likewise:
http://example.com/module/controller/action/param1/value1/param2/value2.xml
Here: $this->_getParam('format') => "xml"
I've fiddled with the default routes, but i cannot get it to work..

You could create a regex route, ending with something like (\w+)(.(\w+))? and capture the part after the . as the .3 capture. see Zend_Controller_Router_Route_Regex

Related

Zend routing: removing null parameters from the URL

If I have a route which is defined like this:
/event/:id/:instance_id/view
It appears in the URL like this when both parameters are set.
example.com/event/1/15/view
However, instance_id is optional, and therefore may be null, with the URL looking like this (as created by the url helper):
example.com/event/1//view
I want it to look like this:
example.com/event/1/view
How do I remove the unnecessary forward slash?
The above question is for Zend 1 and the below solution is for Zend 2, so please dis-regard my answer.
Your route does not say 'instance_id' is optional. You can achieve this by doing this
'route' => '/event/:id[/:instance_id]/view',
It should fix the url problem. Also ideally, 'view' should be after 'event', if it is possible in your scenario.
'route' => '/event/view/:id[/:instance_id]',
Any fix text should ideally come up first in the route and anything optional last.

RESTFUL URLS in rails?

Hi I'm building REST api for an app, I have a requirement in URL
such that url should be something like this e.g
www.abc.com/api/param1/value1/param2/value2/param3/value3.... and so on
There are cases
case: The number of params are not limited it can change frequent
if today it is something like this
www.abc.com/api/param1/value1/param2/value2/param3/value3
tomorrow it can be like this
www.abc.com/api/param1/value1/param2/value2/param3/value3/param4/value4
Is there a configuration where once you configure the url pattern
and every thing go smooth
and in conrtoller params should contain this kind of key-value pair
{ "param1" => "value1","param2" => "value2","param3" => "value3"...and so on }
any suggestion !! how to achieve this ??
If your params are not fixed you can use wildcard in routing
for e.g
get 'items/list/*specs', controller: 'items', action: 'list'
def list
specs = params[:specs] # e.g, "base/books/fiction/dickens" #split it and place in a hash
end
Rails routing provides a way to specify fully custom routes with static and dynamic segments as explained in the Rails Routing Guide.
Your requirement should be achievable with
get '/api/param1/:param1/param2/:param2/...', to: 'controller#action'
You can use route scoping for this particular kind of problem . In other way it is nested routes
More details : http://guides.rubyonrails.org/routing.html#nested-resources
This is a example,
GET /magazines/:magazine_id/ads/:id/edit ads#edit
return an HTML form for editing an ad belonging to a specific magazine
I think this would be helpful for you.

How do I add a query parameter to a Play redirect?

If I construct a Play redirect like Redirect(routes.RegistrationController.register()), how can I add a query parameter to the URL I'm redirecting to?
For example, I'd like a URL like this: /register?token=1234.
You can just do Redirect(routes.RegistrationController.register().url + "?token=1234").
Or, supposing your route declares token as an optional parameter with the route declaration GET /register #controllers.RegistrationController(token: Option[Int]), then you could do Redirect(routes.RegistrationController.register(Some(1234))).
There are various variations to how you could set this up. You may want to see the Play router documentation.

Use route prefix with RESTful routes in CakePHP

Working on building an API and would like to use RESTful routes.
I got it to work just fine like this:
http://www.mysite.com/events.json // returns json results with my events
http://www.mysite.com/events/123.json // returns json results with event of id '123'
BUT - I want to be able to do this using an 'api' prefix.
So, I added the api Routing prefix:
Configure::write('Routing.prefixes', array('admin', 'api'));
And changed my actions from 'view' and 'index' to 'api_view' and 'api_index'.
But now it doesn't work. (eg. I have to write the action name or it won't find the correct one based on HTTP.
The end goal would be to be able to do something like this:
GET http://www.mysite.com/api/1.0/events.json // loads events/api_index()
GET http://www.mysite.com/api/1.0/events/123.json // loads events/api_view($id)
DELETE http://www.mysite.com/api/1.0/events/123.json // loads events/api_delete($id)
...etc
I ended up having to just write the routes manually:
Router::parseExtensions('json', 'xml');
Router::connect('/api/:version/:controller/:id/*',
array('[method]'=>'GET', 'prefix'=>'api', 'action'=>'view'),
array('version'=>'[0-9]+\.[0-9]+', 'id'=>'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'));
Router::connect('/api/:version/:controller/*',
array('[method]'=>'GET', 'prefix'=>'api', 'action'=>'index'),
array('version'=>'[0-9]+\.[0-9]+'));
Router::connect('/api/*', array('controller'=>'events', 'action'=>'index', 'ext'=>'html'));
Notes:
The [method] is what forces the HTTP type (eg. RESTful)
The parseExtensions() makes it so you can have it display the data in different formats automatically by changing the extension in your URL.
The last Router:: line was just a catchall for anything /api/ that didn't match - it forwarded it to the homepage. Eventually I'll probably just route this to an API error page.
The 'ext'=>'html' of the last Router:: line was to keep parseExtensions from trying to use whatever extension was in the URL - if it's redirecting for reasons they made the call wrong, I just want it to go back to the homepage (or whatever) and use the normal view.
Try something like this.
Router::connect('/:api/:apiVersion/:controller/:action/*',
array(),
array(
'api' => 'api',
'apiVersion' => '1.0|1.1|'
)
);
With prefix routing
Router::connect('/:prefix/:apiVersion/:controller/:action/*',
array(),
array(
'prefix' => 'api',
'apiVersion' => '1.0|1.1|'
)
);
Will match only valid API versions like 1.0 and 1.1 here. If you want something else use a regex there.
I know this is an old post, but there is a routing method called mapResources which creates the special method based routing for you.
http://book.cakephp.org/2.0/en/development/rest.html
You put it in routes.php like so:
Router::mapResources(array('controller1', 'controller2'));
The docs have a nice little table showing how the requests are mapped to different actions, which you can always override if you need to.

Why Zend_View_Helper_Url returns relative path when using the default route and absolute path when using a custom route?

I have an application running on Zend Framework 1.12 and I am using the URL view helper. I also have a few custom routes which I use with the URL helper to clean the code.
When I use a format such as this:
$this->url(array('module' => 'myapp', 'controller' => 'index', 'action' => 'index'), 'default', true)
The resulting string is a relative path to the action - /myapp/index/index
But when I use a custom route as such:
$this->url(array('var' => 'value'), 'custom-route', true)
The resulting string is an absolute path - http://www.domain.com/custom-route
I don't understand why is this happening and I can't find any information about this behavior online. Furthermore, I wish to know if there's a way to prevent it from happening and produce consistency in the way the URL view helper works (preferably to always return a relative path).
Thanks.
I think this is as simple as your first example being a default type Zend_Controller_Router_Route_Module route that is normally relative.
Your second example is a named route so it needs to include the base url in order to be valid. I don't believe rewritten routes can be relative as the route may or may not refer to an actual location.
I've done some limited testing and I believe that if you set your baseUrl value the first example will also include the host address. So for consistency your urls will always be absolute.
//application.ini
resources.frontController.baseurl = http://www.domain.com
Hope this helps.