Use a route with the same pattern from another bundle - symfony

I have two routes in two different bundles with the same pattern. As the documentation says, only the first route is used.
route 1 is in a bundle named KitStyleBundle
route 2 is in another bundle (I dont know the name, it could be added by the developpers and represent the application they have to develop)
Question : Normally route 1 is used, but if a route 2 is defined in another bundle, I would like this route to be used. Is it possible to change the order of loading with routes. Or is there any other way to achieve what I want to ?

Routes are not automatically loaded. You can simply change the order of the include statements in your routing config file (app/config/routing.yml)

Related

Why do we need custom route if we can define globally?

If I can add routes in global.ascx or similar file then why do we need custom router i.e. the routes over individual WebApi methods. When it's needed? What is the benefit?
[System.Web.Http.HttpPost]
[Route("api/AndroidOperations/AddManualAppointment")]
public object AddManualAppointment(AddManualBookingViewModel AddManualBookingVM)
{
BookingsRepository BookingRep= new BookingsRepository();
int ReturnRowsCount = BookingRep.InsertManualCustomerAppointments(AddManualBookingVM, out ReturnStatus, out ReturnMessage, out ReturnBookingID);
return new {ReturnMessage= ReturnMessage, ReturnStatus= ReturnStatus};
}
Attribute Routing is a more flexible solution than Convention Routing, if only because it allows you quite a bit more flexibility and places the routes next to the actions that will actually use them.
Consider choosing traditional routes when:
You want centralized configuration of all your routes.
You use custom constraint objects.
You have an existing working application you don’t want to change.
Consider choosing attribute routes when:
You want to keep your routes together with your action’s code.
You are creating a new application or making signifi cant changes to an existing one.
Attribute routing nicely keeps everything about your controllers together, including both the URLs they use and the actions that run. However there are certainly benefits to using both in tandem, particularly in situations when you know how some routes will look but aren't sure about others.
Note: Attribute Routing is more specific and overrides Convention Routing. Be sure attribute routing should be defined first to convention based routing.
To find out more see docs and Attribute Routing vs Convention Routing

symfony 2 annotation routes + group routing

Is there any difference in using annotation routes or group routes in symfony 2 when it comes to performance, convenience, maintainability or in any aspect that would make the other more advisable to use?
Use SensioFrameworkExtraBundle, already shipped with Symfony 2. Having route names and paths in the same place of controller action is the right way to go. If you want to modify a path or action name simply go to that action, without editing routing.yml.
Then give a name to your "grouped routes" e.g. bundle_controller pattern or maybe bundle_entity (if controller is used for CRUD on a single entity):
# app/config/routing.yml
acme_hello_my_annotated_controller:
resource: "#AcmeHelloBundle/Controller/MyAnnotatedController.php"
type: annotation
Eventually prefixing all paths defined by annotations (add prefix: /my/path/prefix).
EDIT: don't know anything about performances. I'm talking only about convenience.

Reuse Bundle Symfony2

This seems like it should be easy, but I am unable to find the answer. How can one reuse a bundle multiple times within the same symfony project? For example if I have an article bundle that I want to use multiple times on the same website.
I see in the app routing.yml you can add a prefix to the routed URL's, however if I try this multiple times with a different prefix each time only the last one works. Assumedly because the unique route names within the bundle are not prefixed, just the routs.
Foo:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bar/
Bah:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bah/
So where do I go from here? Is there some way to auto prefix unique route names, database tables etc (while still being able to reference / link to everything from within templates). Or is this a situation that symfony has just not been designed to accommodate?
I believe that there are two options:
Create the object in the your bundle:
Inside of your foo controller, do something like this:
use Acme\BahBundle\Class;
You should then be able to call it
$class = new Class();
$class->function('params');
The other option is to register the bundle as a service, check out the doc for more info:
http://symfony.com/doc/2.0/book/service_container.html

Removing Specific Routes from ASP.NET 4 RoutesTable

my site has many many routes. since the users can add or remove pages at will, i need a way to reregister the routes from time to time
at reregister i do not want to clear the whole route cache ("RouteTable.Routes.Clear"), but i would rather go thru the table route by route, and using a certain regex on its name, decide if to remove it or not.
after that i will reregister the specific pages that i need to
heres my code meanwhile
For Each r In RouteTable.Routes
If CType(r, Route).DataTokens("ConfigID") = ConfigID Then RouteTable.Routes.Remove(r)
Next
after the first remove it throws an error "Collection was modified; enumeration operation may not execute."
thank you very much for your help!!
It's not possible to get the route name of the route because the name is not a property of the Route. When adding routes to the RouteTable, the name is used as an internal index for the route and it's never exposed.
There's one way to do this.
When you register a route, set a DataToken on the route with the route name and use that to filter routes.
The easiest way to do #1 is to probably write your own extension methods for mapping routes.

Custom catch-all parameter in routing

I recently want to have a special routing rule : {*whatever}/details/{Id}/{itemName}
I know an exception will be thrown once I run the application. In my application, for example my url pattern is www.domain.com/root/parent/child/.../child/details/30/itemname
but the current routing doesnot support this. How can custom the routing handler to make it work?
A class has been written that supports this
I've written such a class that can handle catch-all segment anywhere in the URL. There's quite some code to it, but it works as expected and I've used it on a real life project.
Check it out yourself and see if it fulfils your needs.
The problem is... how will it know when to stop?
the {*whatever} segment will match:
/foo/
/foo/bar
/foo/bar/details/4/moreFoo
/foo/bar/andmore/details/4/moreFoo
Because the catch-all parameter includes anything, it will never stop.
The only way to implement this would be to create a different route for each place you use details...
eg:
games/details/{id}/{itemName}
widgets/details/{id}/{itemName}
books/details/{id}/{itemName}
Of course, that is already provided in the default {controller}/{action}/{id} route
I think you may want to look at extending the System.Web.Routing.RouteBase class and override the GetRouteData() method. With it you can look at the requested url and decide if matches your pattern and if so construct and return a new instance of RouteData that points to the controller and action that you want to handle the request. Otherwise if you don't match the requested url you return null.
See the following for examples:
Pro ASP.NET MVC Framework
By Steve Sanderson
Custom RouteBase

Resources