Symfony 2 redirect route - symfony

I have the following route that works via a get:
CanopyAbcBundle_crud_success:
pattern: /crud/success/
defaults: { _controller: CanopyAbcBundle:Crud:success }
requirements:
_method: GET
Where Canopy is the namespace, the bundle is AbcBundle, controller Crud, action is success.
The following fails:
return $this->redirect($this->generateUrl('crud_success'));
Unable to generate a URL for the named route "crud_success" as such route does not exist.
500 Internal Server Error - RouteNotFoundException
How can I redirect with generateUrl()?

Clear your cache using php app/console cache:clear
return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success'));
If parameters are required pass like this:
return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success', array('param1' => $param1)), 301);

The first line of your YAML is the route name that should be used with the router component. You're trying to generate a URL for the wrong route name, yours is CanopyAbcBundle_crud_success, not crud_success.
Also, generateUrl() method does what it says: it generates a URL from route name and parameters (it they are passed). To return a 403 redirect response, you could either use $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success')) which is built into the Controller base class, or you could return an instance of Symfony\Component\HttpFoundation\RedirectResponse like this:
public function yourAction()
{
return new RedirectResponse($this->generateUrl('CanopyAbcBundle_crud_success'));
}

Related

MVC 5 Multiple Routes to Same Controller

In our RouteConfig.cs file we have the following default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Original", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Path.To.Controllers" }
);
Our application is split into several different "Areas". This particular route works perfectly fine.
We were asked to change one of our URLs, however the underlying codebase is the same. In an effort to avoid breaking existing links out there I'd like to setup my controller to support two different routes:
Here's an example of what the original URL looks like:
website.com/MyArea/Original
With the aforementioned "Default" route in place, this will be directed to the OriginalController in the MyArea Area, and will hit the default Index action since none was specified.
My goal is to setup another URL that will also direct itself to the OriginalController. That is,
website.com/MyArea/Other
Should route to the OriginalController in the MyArea Area, and hit the default Index action.
I've added the following to the RouteConfig.cs file:
routes.MapRoute(
name: "Other",
url: "Other/{action}/{id}",
defaults: new { controller = "Original", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Path.To.Controllers" }
);
What I'm finding is that the Default route config is always used in favor of the Other route config, which causes a binding issue stating "OtherController could not be found". I suspect this is because they have a very similar signature in the url template, but I'm not entirely sure how to get around that.
I'm aware that there's a Route attribute also, which I'm not opposed to using. I unfortunately was unsuccessful in getting that setup correctly though.
After researching and attempting several different combinations I still can't get both URLs to route to one controller.
What am I doing wrong?
I was able to get the expected result using RouteAttribute on the controller itself (thank you #Michael for the resources and making me take another look at the RouteAttribute), rather than conventional MapConfig routing. As I described in my question above, I was having difficulties when attempting the Route approach in that I was receiving 404 errors stating the "resource could not be found".
It turns out the above errors were due to the fact that my attribute routing wasn't being wired up in the correct order, which forced the conventional route to be used (e.g. Default MapConfig) over my Route attributes.
I stumbled upon this SO post which states:
You are probably combining convention based routing with attribute
routing, and you should register your areas after you map the
attribute routes.
The line
AreaRegistration.RegisterAllAreas(); should be called AFTER this line:
routes.MapMvcAttributeRoutes();
When working with Areas, you must register those areas after you register the attribute routing. I was originally registering my areas in the Application_Start method of Globas.asax.cs, which is called before the RouteConfig.RegisterRoutes. I moved this registration to right below my MapMvcAttributeRoutes call in the RouteConfig.cs file, which allowed the following route attribute on the controller to work as expected:
[RouteArea("MyArea")]
[Route("Original/{action=index}", Order = 1)]
[Route("Other/{action=index}", Order = 0)]
public class OriginalController : Controller {
...
...
public async Task<ActionResult> Index() { ... }
}
With the above in place, I can now navigate to either of the below URLs which will properly route to the "Index" action of my OriginalController:
website.com/MyArea/Original
website.com/MyArea/Other
This works. However, I do have another action defined that choked up the attribute routing and caused the conventional Default route (defined via the MapConfig function) to be used. My action signature:
public async Task<ActionResult> Details(int id) {
...
}
The route to this action is: website.com/MyArea/Original/Details/123, which also satisfies the default conventional route of {area}/{controller}/{action}/{id}.
The way around this was to go a step further with defining route attributes at the action level:
[Route("Original/Details/{id:int}")]
[Route("Other/Details/{id:int}")]
public async Task<ActionResult> Details(int id) {
...
}
Now my Route Attributes are found first, then the conventional route is used.

Symfony redirect to route passing url slug

I've searched this on SO previously, but it seems that the only answers given are when parameters are passed as a query string, which is not what I want.
I have a page in my Symfony 3 CRM with a route app_litter. There is a required URL slug called litter_id which needs to be passed to the route in order to determine which litter data to show, as follows:
/litter/1
My route definiton in the routing file is:
app_view_litter:
path: /litter/{id}
defaults: { _controller: AppBundle:Litter:view, id: null }
requirements:
id: \d+
There is a function which allows a user to add puppies to their litter, which is a form outside of this page - once the puppies are successfully saved, I want to redirect the user back to the litter in question, so I did this:
return $this->redirectToRoute('app_litter', array('litter_id' => $litter->getId()));
Where $litter is the object retrieved from Symfony. However, this redirects to:
/litter?litter_id=1
Which does not work, as it expects a slug. Is it possible to use redirectToRoute() (or any other method) to render the route with a slug instead of a query string?
Because your route definition is:
app_view_litter:
path: /litter/{id}
defaults: { _controller: AppBundle:Litter:view, id: null }
requirements:
id: \d+
When using the route generator you need to provide an associated array with keys corresponding to the name of your placeholders which is in your case id.
Therefore, it must be:
$this->redirectToRoute('app_litter', array('id' => $litter->getId()));
If you want to use a slug, and there something which not only composed of digits (\d+), you have to either define a new route or modify the existing.
app_view_litter_using_slug:
path: /litter/{slug}
defaults: { _controller: AppBundle:Litter:view, slug: null }
requirements:
id: .*
And you something like:
$this->redirectToRoute('app_litter_using_slug', array('slug' => $litter->getSlug()));
Could it be that you are using the wrong route? Try using app_view_litter instead of app_litter:
return $this->redirectToRoute('app_view_litter', array('id' => $litter->getId()));

trying to use params on main route (/) of my meteor app

I'm trying to use params on my main route. But in fact the params are not set and they are used in the path :
Router.map(function funcClientRouterMap(){
this.route('home', {
path: '/:_redirect?',
action: function funcClientRouterMapAction(){
console.log(this.path, this.params);
}
})
});
now if i try manual redirection here is what i get :
Router.go('home'); // it redirects on / => ok
Router.go('home', {_redirect: test}); // this.path = /test, and this.params is empty
How can i use _redirect like a params and not a route ?
Thanks
Router.go accepts a path as its first argument (per the docs). So if you were trying to programmatically cause the same result as the user going to /redirectMeSomewhere, you just use:
Router.go('/redirectMeSomewhere');
And this.params._redirect should be 'redirectMeSomewhere'.
Note that like #apendua implies, if you have other routes it could cause chaos to have a route defined as /:anything, because the other routes may never get triggered. If the above doesn't do the trick, try commenting out all your other routes to see if that changes anything.

Symfony2, no route found and defaults

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.

Symfony2 template configuration from routing.yml

When dealing with the Sylius e-commerce bundles I have found what seems to be a way of configurig the template for a route, that I didn't know:
I have tested in a fresh Symfony RC 2.2.0 with vendors installation.
This would be in the routing.yml
_welcome:
pattern: /
defaults:
_controller: AcmeDemoBundle:Welcome:index
_template: AcmeDemoBundle:Welcome:index # added by me
this generates an error:
FatalErrorException: Error: Call to a member function getTemplate() on
a non-object in
.... \vendor\sensio\framework-extra-bundle\Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener.php
line 62
now, in TemplateListener, what we have is:
if (!$configuration = $request->attributes->get('_template')) {
return;
}
if (!$configuration->getTemplate()) {
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$configuration->setTemplate($guesser->guessTemplateName($controller, $request, $configuration->getEngine()));
}
$configuration is a String, actually the template I put in routing.yml (AcmeDemoBundle:Welcome:index). Checked by adding a var_dump and also inspecting ParameterBag -> get method which is what $request->attributes is.
So. Why TemplateListener is expecting an object? What am I missing? Am I missconfiguring in routing.yml?
This parameter is not available in Symfony itself.
Feature is provided by SyliusResourceBundle and available only in Sylius controllers.
And apparently _template request attribute conflicts with SensioFrameworkExtraBundle, which uses same name to store object.
We have to move those parameters one config node deeper, to avoid such problems in future.
You can keep an eye on the https://github.com/Sylius/SyliusResourceBundle repository, fix should arrive today.

Resources