many routing for same controller action - symfony

I use symphony ยง and I manage my routes in routing.yml function,
How can I use mutliples routes for same controller function. I know I can do it with annotation but I don't manage my routes in annotation.

You can have multiple routes for the same controller in routing.yml as in annotations.
If there are multiples routes matching the url, the firt one win, as explained in the doc.

Related

Asp.Net MVC custom routing interrupt default routing

My routes are not working don't know why. When I try attribute routing it works for for that action but breaks default routing for other controller or actions. I only want to change routing for an index action method of a controller CMS I want it like [Route("{countrycode}/{id}")]. but when I do this default route breaks for other index action methods of other controllers.
For example: When I use [Route("{countrycode}/{id}")] on cms controller's index action then route breaks for Home and other controllers index action methods so I have to call the index methods explicitly like /Home/Index instead of /Home otherwise it throw an error. Thanks in advance.

Symfony route without controller

I have to work on a Symfony project that I never met before. There is a route that I'd like to figure out what script generates the result. Normally int the routing.yml the the _controller in the defaults tells this, but this route does not have any controller associated with:
vendorname_admin_generate_external_site_admin:
path: /admin/
host: "{current}.%vendorname_academic_base_host%"
defaults: {current: %vendorname_academic_base_subdomain%}
The two parameters are:
vendorname_academic_base_host: vendordomain.com
vendorname_academic_base_subdomain: developmentserver-vpn
Could you help me to understand what does this route definition do?
Because of the fact it is defining a specific host (ie, a domain name) as part of the route, makes me think that it's just an alias to an external website, that isn't actually in the same codebase. -- Also the fact its called 'external_site_admin'.
I've just added a similar route with a specific host, mine is still in the same codebase, but under a specific, shorter URL, but I defined my route as a template that I can use to setup as an alias.

Symfony2 Routing: import all controllers' annotations BUT exclude one (duplicated routes in NelmioApiDoc)

The problem
When we define in routing.yml:
my_controllers:
resource: "#MyBundle/Controller/"
type: annotation
prefix: /
and:
my_api:
resource: 'MyBundle\Controller\ApiController'
type: rest
prefix: /api
name_prefix: my_api_
options:
expose: true
We get duplicated routes in NelmioApiDoc:
The question
How we should do this so routes from ApiController won't be duplicated?
exclude ApiController from "wildcard" resource import?
import all other controllers individually?
other solution?
You get duplicated entries in Nelmio ApiDoc because there are actually duplicated routes in Symfony. Execute this command to see every route in Symfony, including those of your API (that will appear both with the /api prefix and without it):
php app/console router:debug
As Yann suggests, I think the best approach is to create a separate directory for your API Controllers (something like #MyBundle/ControllerRest/) and place them there. Then, don't forget to update the resource route under my_api section in your routing.yml and the namespaces in your API Controllers.
This way you get rid of the duplicated routes and thus ApiDoc will only show one route per call, instead of two.

Convention-based routing in Symfony2

I'm trying to learn learn how routing works in Symfony2, and so far everything I've read has examples like this:
blog:
path: /blog/{page}
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
This routes requests to /blog/123 to the AcmeBlogBundle Blog controller's "index" action, and passes the 123 parameter as the "page" parameter to that controller action. If no page parameter is passed, then the page defaults to 1.
That's all well and good, but what if you want to simply have a convention based routing system that passes things through like this:
/{bundle}/{controller}/{action}
So, for a URL like this:
/acme/blog/index
It would then call AcmeBlogBundle Blog controller's "index" action.
No specific routing configuration is necessary, it simply infers the bundle, controller, and action from the URL. So you can continue adding bundles, controllers, and actions, and you don't need to modify the routing configuration. It just works.
If this isn't possible, can you at least infer the controller and action from the URL? E.g., perhaps you need a route that specifically identifies the bundle, but can we get the controller and action from the URL?
I read through the Symfony "The Book" page about routing, and I couldn't figure out a way to do this.
No way. This was considered as bad practice and so it was removed from symfony.
But you should take a look at the #Route annotation, as it simplifies configuring routes in such a nice way. Directly attached to the action, there is no lack between config and code.

Managing routing while extending a controller

I am trying to override a controller that I use in Frontend:
AcmeDemoBundle:DemoController //route is /demo
For the backend, I extended this controller in AdminDemoBundle:
AdminDemoBundle:DemoController //route is /admin/demo
I haven't overriden any methods in the new controller yet, but all the routes from AcmeDemoController go to /admin/demo.
Does anyone know why?
Is there a solution to override frontend controllers for backend purposes?
Basically, by extending the controller, all the routes are redefined with the same name and a different prefix, which means that the routes are being replaced.
There is actually no point at all in extending a controller for the backend, as controllers are meant to be tiny. The logic of the app must be kept in services instead.
Moving the logic to a service allows a better organisation of the code. There is no need to worry about backend/frontend when talking about services. Just, create the logic in a service, and use the methods in either a frontend controller or a backend controller.

Resources