Symfony2, no route found and defaults - symfony

Simple question but I have issues with it.
We have simple route
profile_api_info:
pattern: /api/info/{apiID}
defaults: { _controller: SiteProfileBundle:Api:info, apiID: null}
When we use such url as
http://some.site/api/info/123
we'll get proper result of controller.
But when we use this one
http://some.site/api/info/
we'll have an error, why?
No route found for "GET /profile/api/info/"
We'll have already setuped 'defaults' for our 'apiID' but symfony2 says that no route. Can someone suggest how to deal with it? I want routes
http://some.site/api/info
http://some.site/api/info/
have the same controller as
http://some.site/api/info/123
but with 'apiID' = null or false, no matter.

You have two options.
Option 1:
Pass your default parameter.
profile_api_info:
pattern: /api/info/{apiID}
defaults: { _controller: SiteProfileBundle:Api:info, apiID: null}
However you will not be able to have trailing /.
This would be correct: http://some.site/api/info
This would be incorrect http://some.site/api/info/
Option 2:
set up an additional route.
(This would be my preference.)
profile_api_info_woId:
pattern: /api/info/
defaults: { _controller: SiteProfileBundle:Api:info}
In your controller make sure set the default for $apiID to null.
public function infoAction($apiID = null){...}
Using two routes with one controller method should work for all of the following urls:
http://some.site/api/info
http://some.site/api/info/
http://some.site/api/info/123

I've ran into this multiple times and clearing the cache so that Symfony can rebuild the route definitions usually fixes the issue.
Syntax and everything looks correct. However, the route with the trailing slash (http://some.site/api/info/) won't work but http://some.site/api/info should.

Related

symfony routing, use value from Request as default

I've got the following route definition
my_route:
path: /actual-path/
defaults:
_controller: MyBundle:MyController:detail
id: application_id
requirements:
methods: GET
id: \d+
The controller requires a parameter called $id.
But I don't want to use the $id in the url, I want to use a value that is available in $request->attributes->get('application_id')
There is a listener that will inject two parameters (application_id and application) into the request object as attributes prior to the routing process, so this value is in there. (It would be easy to also inject it into the RequestContext).
Is there a way I can use attributes values from the Request or RequestContext object in my routing as defaults?
Now I could simply do $request->attributes->get('application_id') in my controller. But this controller will be used in several cases. In other cases the $id is to be passed through the url. I find it cleaner to set the id in the routing than build a if-else clause in the controller somewhere.
It appears not to be possible to do "mapping" between variables in the request object and the parameters you need to be required in your route/controller-action. I think this would be a good thing, as it would become quite complex otherwise.
I went with the solution to extend the controller and build in a small switch there.
Basically, if the route specifies id === null, it will do a fallback to application_id that is in the Request object. Otherwise it will use the value provided. I just need to set a requirement on id on the routes that must not use the fallback.
All without running an additional Listener which might be a bit "expensive" in processing time (for each request).
Example of routes:
my_route:
path: /actual-path/
defaults:
_controller: MyBundle:MyController:detail
id: null
requirements:
methods: GET
my_other_route:
path: /other-path/{id}
defaults:
_controller: MyBundle:MyController:detail
requirements:
methods: GET
id: \d+
And how to handle this in your controller:
// fallback to system id
if ($id === null) {
$id = $request->attributes->get('administration_id', null);
}
Because you extend the controller/action, you could also change the name of the parameter in the action (as long as the type does not change).
I did not do this, as I could quite easily put the switch between the provided id and the fallback from the listener in another method.

Sylius DisplayAction Requirements

i need for Sylius displayAction a requirement like that:
foo_display:
pattern: /{slug}/{stepName}
defaults: { _controller: sylius.controller.process:displayAction, scenarioAlias: 'foo_ordering', _menu_item: foo_order }
requirements:
slug: micropage|standard
But this not work for me. It will be exit with the follow exception:
The "foo_display" route has some missing mandatory parameters ("slug").
500 Internal Server Error - MissingMandatoryParametersException
How i can fix this? I don't want to add the same route for "standard". I will use one for two ;)

cannot import resource in symfony2

I am new to symfony2. I am designing a form.
My action for submission is task_new. And my routing.yml is as follows:
task:
pattern: /task/
defaults: { _controller: AcmeTaskBundle:Task:new}
task_new:
defaults:{_controller:AcmeTaskBundle:Task:sub}
I want that after submission the form it should go to sub action. when i am running this code I am getting the following error:
Cannot import resource "C:\wamp\www\Symfony\src\Acme\TaskBundle/Resources/config/routing.yml" from "C:/wamp/www/Symfony/app/config\routing.yml".
What should I do?
Such error usually appears when you have error in your resource file.
In your case - I guess, you missed the pattern for task_new route
Also check if defaults starts right after 4 spaces from the beginning
task_new:
defaults: { _controller:AcmeTaskBundle:Task:sub }

How do I use Period in Route in Symfony2?

I'm trying to have a route in symfony that matches the following url:
/filename/version/
where filename is an image, say "image.png" and version as say "foo".
MyImageBundle_resize:
pattern: /{filename}/{sizename}/
defaults: { _controller: MyImageBundle:ImageResize:resize }
and this matchs the pattern /myfile/XXL/, for example. But when I use myfile.jpg, as in /myfile.jpg/XXL/ Symfony seems to break on the period, and returns 404. I found this article which suggests that everything except the "/" will match (which doesn't make any sense if the period is breaking here).
Is there a way I can have the route match on the period?
You don't need to allow slash in URL. Just use simple route:
file_check:
pattern: /check/{filename}/{version}
defaults: { _controller: AcmeDemoBundle:File:check }
And in your controller use:
public function checkAction($filename, $version)
{
//some action
}

Two optional variables in Symfony2 routing

When I try to use 2 optional variables in Symfony2 routing I have th error: No route found for "GET /"
In routing.yml I have:
AcmeAshavatBundle_homepage:
pattern: /{page}/{ads_on_page}/
defaults: { _controller: AcmeAshavatBundle:Page:index, page:1, ads_on_page:2 }
requirements:
_method: GET|POST
And when i go to http://localhost:8080/AshavatSy/web/app_dev.php/ I have the error. The intresting is that if I run http://localhost:8080/AshavatSy/web/app_dev.php/1 it works well.Also, if I change the path to pattern: /main/{page}/{ads_on_page}/ it works well.
What is the problem?
I'd like to ask, that someone will try to do like this [e.g. pattern: /a/b/ defaults: {... a:1,b:2}, or as he thinks you should do it] in his project, and see is it a common problem...
I managed to have something similar working by defining two routes, pointing to the same controller, using default parameters. In my case, using annotations:
/**
* #Route("/products/{catId}/{prodId}", defaults={"catId"="", "prodId"=""})
* #Route("/products/")
* #Template()
*/
public function indexAction($catId = null, $prodId = null) {
...
I think that using default parameters only, Symfony would expect two /.
HTH
I think you forgot to pass these two arguments to your IndexAction() in controller.
Try this code
Public function indexAction($page,$ads_on_page)
{}
Hope this helps you.

Resources