How to set default controller and action in symfony2? - symfony

How can I set default controller and action in symfony2. Where to configure this settings.

Which annotation do you use?
My default Route is in the file app/config/routing.yml
_index:
pattern: /
defaults: { _controller: ACMEUserBundle:Default:index }
If you have installed with DemoBundle there is a Default Route in the file src/Acme/DemoBundle/Resources/config/routing.yml.

Look the Link it's show some idea to you
http://symfony.com/doc/current/book/routing.html
cant run the controller with symfony 2

Related

Why Symfony generate 2 routes when I use annotation routing type and subdirectory?

I use symfony 4 and the annotation type for routes.
I have an Admin directory in my Controller directory.
In the Controller directory, I have 3 controllers with some routes
And in my Admin directory, I have EmployeeController (prefixed by "/employee" and named by "employee_") with 3 routes (index, new, edit)
My annotations.yaml file look like this:
admin:
resource: ../../src/Controller/Admin/
type: annotation
prefix: /admin
name_prefix: admin_
trailing_slash_on_root: false
public:
resource: ../../src/Controller/
type: annotation
trailing_slash_on_root: false
After a bin/console debug:router I have :
admin_employee_index GET ANY ANY /admin/employee/
admin_employee_new GET|POST ANY ANY /admin/employee/new
admin_employee_edit GET|POST ANY ANY /admin/employee/{id}/edit
employee_index GET ANY ANY /employee/
employee_new GET|POST ANY ANY /employee/new
employee_edit GET|POST ANY ANY /employee/{id}/edit
dashboard_index GET ANY ANY /dashboard/{year}/{week}
schedule_index ANY ANY ANY /schedule/{year}/{week}
localization_index ANY ANY ANY /localization/{id}
It's possible to exclude a controller or a directory that are already configured ?
Because symfony generate 2 routes for the same controller.
Of course I can use a single config for routing (public in this example) and set prefix for admin controller with "/admin/employee" but I don't like this method. That's mean that every controller need this config.
You cannot exclude a directory from routing definition. As a better way of separating controllers you could create AdminController directory in src and move all admin controllers there.

Add optional _locale to routes

I'd like to prefix all my urls by an optional _locale. BTW, I'm handling fr and en.
The point is, when _locale is given I'll use it thanks to symfony Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest, if not I'll use the locale saved in the database thanks to my own listener (priority: 15) which runs immediately after the previous one.
What I've tried so far is the following:
app/config/routing.yml
entrypoint:
resource: 'locale_prefixed_routing.yml'
prefix: /{_locale}
And locale_prefixed_routing.yml imports bundles routings.
/fr/url/example works but /url/example does not and returns 404 page.
How do I tell symfony to use the locale in the url, if does not exist use the one in the database?
I suppose your routes are defined like this in your locale_prefixed_routing.yml :
my_route:
path: /
defaults:
Actually routes like /url/example without any locale are not defined in your routing because you ask to prefix all the routes loaded in locale_prefixed_routing.yml by the locale. So it is normal to get an error when requesting such a route without locale.
You can try to make another routing file named non_localized_routing.yml containing the same routes except that you will use specific defaults.
Example :
route_example:
path: /url/example
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/url/example
permanent: true
Tell me if it permits you to get rid of your problem, I haven't test it yet.
For me it sounds weird to store the locale in the database when the locale is not provided by the user. You can simply add a default one:
framework:
default_locale: en

Symfony2 & EmberJS: how to make both prod and dev (/app_dev.php/...) urls working?

Say my application currently responds to the /products route just fine. How do I setup Ember.JS so that /app_dev.php/products would be the same route?
If I understand your problem right, you need to generate Symf route via js instead of static "/products" route?
If so you should use FOSJsRoutingBundle https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
Just set option "expose" to your routing as:
app/config/routing.yml
my_products:
pattern: /products
defaults: { _controller: ProductBundle:Products:index }
options:
expose: true
then you can generate the route depends on env from javascript as:
Routing.generate('my_products')

SonataAdminBundle - How to change route to dashboard?

I have installed Symfony 2.2.0 with SonataAdminBundle. Now I'd like to change default dashboard route from /dashboard to /.
How to do it in a right way?
You can use as below into your app/config/routing.yml file:
default:
pattern: /admin/
defaults: { _controller: SonataAdminBundle:Core:dashboard }
You can try this
Create a copy of the sonata route file
https://github.com/sonata-project/SonataAdminBundle/blob/master/Resources/config/routing/sonata_admin.xml
and make necessary changes. Save the file in one of your bundles
In your app/config/routing.yml refer to this route file
admin:
resource: '#AcmeAdminBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin

Ordering of routes using annotations

The usual solution when you create routes in symfony and you want to have one route like
/{username}
so that it does not conflict with other routes like /login or /info is just to put that route as your last route in your routing.yml file. Since all the other routes take precedence this conflict is avoided. But how can you do this if you define your routes as annotations in your controllers? Is there any way to specify the ordering of this routes in this case?
In the context of a controller, the order of action methods defines the order of routes. In the context of the whole application, you can import each controller explicitly to control the order, for example:
Home:
resource: "\Vendor\Controller\HomeController"
type: annotation
Security:
resource: "\Vendor\Controller\SecurityController"
type: annotation
security.log_out:
pattern: "/logout"
User:
resource: "\Vendor\Controller\UserController"
type: annotation
I can't comment on the answer, so I will leave how I had to write it in Symfony 2.3 to get it to work:
Home:
resource: "#AcmeBundle/Controller/HomeController.php"
prefix: /home #optional
type: annotation
Notice the change of "\" to "/" and using .php at the end of controller name.
You need to overwrite the rule for that route at the end of your routing.yml. You can do this by using the same name for the route as the one that is automatically created by the annotation. You can find the name with the console command:
php app/console debug:router
So in your routing.yml as the last line you will add:
the_name_of_the_route_as_found_with_appconsole_debug_router:
path:/{username}
defaults: { _controller: YourBundle:YourController:the_action_to_use }
By using the same name it is given by default you will override the original. Don't forget other options that might be important. Like a default value or null for {username} or the method.
You could also remove completely the route from the annotation for the controller. It is not required anymore as it is overwritten anyway :)

Resources