Symfony2 - FOSUserBundle - Add _locale in url - symfony

I would like to overwrite default routing of FOSUserBundle for Symfony2 to get a result like this:
www.mysite.com/en/login
www.mysite.com/es/login
www.mysite.com/fr/login
www.mysite.com/en/profile
www.mysite.com/es/profile
www.mysite.com/fr/profile
Thanks for your help.

Look at this link. I think it is exactly what you need.
[...] to add a prefix before the _locale string (e.g. /admin/en/dashboard), you can add the “i18n_prefix” option.
# app/config/routing.yml
dashboard:
...
options: { i18n_prefix: admin }

Related

Unable to find template Bundle:Default:index.html.twig

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

Symfony - Defining a route only in dev. environment

I'm using SF2 and I created some routes helping the debugging of the project:
widget_debug_page:
path: /debug/widget/{widgetName}
defaults: { _controller: WidgetBundle:Debug:default }
The problem is that this route MUST never be reachable when going in production.
I could not find a parameter to specify the environment.
Just for people who want use with annotation system:
/**
* #Route("/my-route", condition="'dev' === '%kernel.environment%'")
*/
public function index() {}
You can add your route in routing_dev.yml (Symfony 2/3) or create a dev-only routing file in config/routes/dev/ (Symfony 4).
As mentioned perviously you can define the conditional route by adding 'condition' directive to the route definition. In case you need to limit the route for the appropriate environment you can check %kernel.environment% parameter value directly. For example:
widget_debug_page:
path: /debug/widget/{widgetName}
defaults: { _controller: WidgetBundle:Debug:default }
condition: "%kernel.environment% === 'dev'"
I just had a similar problem myself since I wanted to create a "System temporarily down due to maintenance"-page that would disable my site entirely in prod-environment only as I use dev-environment to make sure everything is alright after deploying an update to my production server. I guess its debatable whether this is good practice, but anyway...
The comment made by Leto to a previous answer gave me an idea how to solve it and I think I have successfully managed to make my route only work in dev-environment like so:
default_update_view:
path: /{route}
defaults: { _controller: WalanderBundle:Default:maintenanceInfo }
condition: "request.getScriptName() == '/app.php'"
requirements:
route: .+
No the question was to only enable a route in dev-environment which would then be done by changing the condition as follows if I'm not missing anything obvious:
condition: "request.getScriptName() == '/app_dev.php'"
Hope this helps although the question was a bit old!
Perhaps in the case when you want to enable a route in the dev-environment only the already provided answer by JonaPkr is best practice though.
Symfony >= 5.3
when#dev:
widget_debug_page:
path: /debug/widget/{widgetName}
defaults: { _controller: WidgetBundle:Debug:default }
reference: https://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file

Products sorting on indexByTaxon page

I'm trying to add ability to sort products by price and date. Are there any predefined methods to do that, or the only way is to implement them by hand? From sylius.yml we're getting such strange route:
%sylius.model.taxon.class%:
field: permalink
prefix: /t
defaults:
controller: sylius.controller.product:indexByTaxonAction
sylius:
template: SyliusWebBundle:Frontend/Product:indexByTaxon.html.twig
Which can be used like {{ path(taxon) }}. But just adding sorting parameter doesn't work for me. Any ideas?
You need a version, which incorporates the Pull Request #2122. Either the latest master, or you fork the 0.11 branch and cherry pick this fix.
Then you can simply define in your config.yml to only override the required defaults:
sylius_core:
routing:
%sylius.model.taxon.class%:
defaults:
sylius:
sorting:
order: desc

Symfony 2.5 SEO Friendly URLs

I'm trying to build my own CMS based on Symfony 2 since I swapped jobs and now require some symfony practice, but got stuck when trying to do something sensible with routes. In Zend it was possible with preDispatch so I hope Symfony 2 will have something similar too.
Consider this URL: http://www.example.com/page_one/sub_page/another_sub_page/yet_another_sub/
What I am trying to achieve is to have http://www.example.com/page_one/ matching to some controller and getting me the content of the page that has the url_string parameter same as that portion of URL. Then http://www.example.com/page_one/sub_page/ should also match to the same controller but render content for the sub_page portion of URL.
The route must match n portions because I cannot know how deep the structure might go.
So I will not accept something like
/{slug1} or
/{slug1}/{slug2} or
/{slug1}/{slug2}/{slug3}
as that is nonsense.
I am planning to make this route as last one in the routing config so should there be some matches on other routes they will be picked up instead, and this one despite it is most important route will be last resort matching kinda thing.
Using the following config will match all urls, and the entire path will be passed into the controller "slug1/slug2/slug3".
SymfonyBundle/DefaultBundle/Resources/config/routing.yml:
symfony_default_page:
path: /{path}
defaults: { _controller: SymfonyBundle:Default:page }
requirements:
path: "^.+"
Or** ou can also use regex in the requirements path. This will exclude "admin", "login", "blog", however it's much easier if you simplify things and just place the routing in the correct order like you said
requirements:
path: "^(?!admin|login|blog).+"
app/config/routing.yml
By importing the default bundle routing last, it means any routing yml files above will take priority and therefore not match the default route which will catch any url.
SymfonyAdminBundle:
resource: "#SymfonyAdminBundle/Resources/config/routing.yml"
prefix: /admin
#import this last
SymfonyDefaultBundle:
resource: "#SymfonyDefaultBundle/Resources/config/routing.yml"
prefix: /
Controller:
class DefaultController extends Controller
{
public function pageAction($path)
{
//You could split the URL and handle accordingly, also filter / escape?
$parts = explode("/", $path);
}
}

What does _sonata_admin: {resource: . } do in routing.yml?

When setting up SonataAdmin, you have to add an entry like this one:
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
In this entry, what does resource: . do? It's somewhat unusual syntax and I'd like to add a footnote about it to the SonataAdmin docs.
The routing component needs a resource attribute (at least with sf2.0). The AdminPoolLoader class is in charge on loading the route and the support method only check the type attribute and not the resource name provided ....
So the dot is just a value that others RoutingLoader cannot validate and generate errors...

Resources