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.
Related
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
I'm seeing some places where there is an "options" property on routes (below is taken from https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/doc/index.md#generating-uris):
my_route_to_expose:
pattern: /foo/{id}/bar
defaults: { _controller: HelloBundle:Hello:index }
options:
expose: true
Note the "options" key under "my_route_to_expose".
While this obviously exists, I cannot see any place in the Symfony documentation that mentions this. I've also tried poking around the code in the project that the above example came from, but cannot seem to find where they are picking up on this at all.
From what I can assume (since I see no documentation for it) is that it can be used to store just arbitrary data with a route that you as the developer can pick up on and use, however, I don't know when and where you would be using this information at.
So, what can this be used for and in what context can it be accessed?
That key is used by FOSJsRoutingBundle and is not a part of Symfony2 core.
By default FOSJsRoutingBundle doesn't expose all your route (I think for performance & security reasons) but only those that have that key set to true.
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)
I'm working on a Symfony2 project and am trying to figure out how to pass parameters from the route configuration to the controller. I know I can configure default values in the route configuration, and retrieve the values in the controller using the appropriate var name in the function declaration, but that isn't exactly what I want.
My use case is the following. I have a standard method in my controller that I want to access from 2 or 3 different routes. Depending on which route is being called, I want to "configure" the method differently. I can accomplish this in a few ways:
In my controller, check the route name using `$this->container->get("request")->get("_route"), but that is ugly, and then I am hardcoded to the route name. Moves configuration to the controller, which should just be logic - not configuration.
Create a base controller class, and subclass each method for my different routes. Each subclassed method would then have the necessary configuration within the method. Cleaner soln than #1, but still "heavy" in the sense of having multiple classes for a simple need and still pushes configuration data into the business logic.
Put configuration data into the route configuration. In the controller, access the configuration data as required. Ideal solution, but don't know how.
I can use the route default array to specify my arguments, but then must make sure to use a regex to ensure that the params are not overridden at the URL level (security risk). This is functional, but still kinda cludgy and not a pretty hack.
I presume that there must a better way to do this, but I can't seem to figure it out. Is there a way to access the routing object from the controller, and access the different configuration parameters?
You can pull the actual route from the router service. Something like:
$routeName = $this->container->get("request")->get("_route");
$router = $this->container->get("router");
$route = $router->getRouteCollection()->get($routeName);
Not sure if this would be such a great design though. Consider passing a $configName to your controller method, adding a parameter with the same name in a config file then using getParameter to access it. That would eliminate the route stuff from the equation.
Something like:
zayso_arbiter_import:
pattern: /import
defaults: { _controller: ZaysoArbiterBundle:Import:index, configName: 'someConfigName' }
public function importAction(Request $request, $configName)
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