Twig path without default locale - php

in my Symfony 3.3 project I use locale system to change the user language. I have configured my routing to allow the 'en' version for not having it in the url.
acme_front_office:
resource: "#AcmeFrontOfficeBundle/Resources/config/routing.yml"
prefix: /{_locale}
defaults: { _locale: 'en'}
requirements:
_locale: '|en|fr'
So those url redirect all to the same page :
/home
/en/home
/fr/home
But in my twig templates the {{ path() }} function set by default the right locale except that when my user see the english version of the website I want that the generated url head to /home and not /en/home.
If you have a clue on how to do that without changing all the path() calls to override the _locale param I will be very thankful.
Have a good day ;)

So after some digging, the solution that I came up with is to rewrite some parts of the Symfony Routing system. If you want to check the solution you can find the code in the following gitlab repo, in the Routing dir.
TranslateBundle Repo

Related

What Does the `type:` Configuration in a Symfony Route Configuration Control?

What does the type: configuration in a Symfony routing file control? What are its valid values?
I can't find this configuration field explicitly documented anywhere. It's referenced indirectly in Symfony's routing documentation.
app_directory:
resource: '../legacy/routing/'
type: directory
and seems related to loading in additional routes. However, its behavior (or all its allowed values) doesn't seem to be explicatly defined anywhere. I can make a guess that it somehow tells Symfony how to load the external routes, but I'd love to know
Is my guess correct?
Are there valid values other than directory or annotation?
Is this formally documented anywhere?
Is there a spot in the Symfony internals that would be a good place to start finding these answers for myself?
You can find how the type works in the Symfony documentation, see code below. It controls if the routes should be loaded from the PHP annotations or the YAML or XML files found in that (bundle) directory.
app_file:
# loads routes from the given routing file stored in some bundle
resource: '#AcmeOtherBundle/Resources/config/routing.yaml'
app_annotations:
# loads routes from the PHP annotations of the controllers found in that directory
resource: '../src/Controller/'
type: annotation
app_directory:
# loads routes from the YAML or XML files found in that directory
resource: '../legacy/routing/'
type: directory
app_bundle:
# loads routes from the YAML or XML files found in some bundle directory
resource: '#AppBundle/Resources/config/routing/public/'
type: directory

Symfony2 FOS Rest bundle routing FileLoaderLoadException controller

I have a very strange issue, with my Symfony2 setup.
I'm working on a restful webservice and would like to setup routing.
I have a fully working application and woud like to change my routing.yml config.
Working configuration
my_product:
resource: My\Bundle\ProductBundle\Controller\DefaultController
type: rest
prefix: /
When I change that to:
my_product:
resource: "#MyProductBundle/Controller/"
type: rest
prefix: /
I get the following error:
Symfony\Component\Config\Exception\FileLoaderLoadException"
message="Can't find class for controller
"#MyProductBundle/Controller/" in #MyProductBundle/Controller/ (which
is being imported from
"/home/myproduct/domains/example/v5/app/config/routing.yml"). Make
sure the "MyProductBundle" bundle is correctly registered and loaded
in the application kernel class. If the bundle is registered, make
sure the bundle path "#MyProductBundle/Controller/" is not empty.
When I change the type from "rest" to "annotation", the error disappears.
What am I doing wrong? I can't find it out and my searches lead to nothing.
Many thanks in advance!
You can't currently import all of a bundle's controllers at once when using FOSRestBundle. It will be added in FOSRestBundle 2.0. Import your controllers individually like in your first example.

In Symfony2.0, I want to disable some url in router.yml when using app.php (prod env)?

When using app_dev.php, there are some useful url for me to debug project. Such as
check_debug:
pattern: /check_url1
defaults: {....}
check_debug_and_prod:
pattern: /check_url2
defaults: {....}
check_prod:
pattern: /check_url3
defaults: {....}
I want to disable 'check_debug' when I using app.php(prod env), and disable 'check_prod' when I using app_dev.php(dev env), and keep 'check_debug_and_prod' both when using app.php or app_dev.php.
How to config in SF2?
You can have multiple routing.yml files for different environments. E. g. app/config/routing.yml, app/config/routing_dev.yml
You can have different routing files for different environments:
Create src/AAA/Bundle/CCBundle/Resource/config/routing.yml
Create src/AAA/Bundle/CCBundle/Resource/config/routing_dev.yml
Include src/AAA/Bundle/CCBundle/Resource/config/routing.yml in app/config/routing.yml
Include src/AAA/Bundle/CCBundle/Resource/config/routing_dev.yml in app/config/routing_dev.yml
You can use the routing_dev.yml to add routes you only want in the dev mode. When you will use app.php it will only check for routing.yml and this way you will not have to be preoccupied by the unwanted routes.
You can also create your own routing files. See how Sf2 handles to import inside yml files.

Access external directories from symfony2

I have images that are stored in a directory in /var/lib/myproject. So they are outside of my symfony project. I would like to display them in a twig template or via css.
How could I access them as symfony is catching every url and rewrites them?
Setting a specific route doesn't work :
my_project_res:
resource: "/var/lib/myproject"
prefix: /res

How to create the route in symfony 2 which maps to external URL?

Referring to this,
http://symfony.com/doc/current/book/routing.html
we can map url pattern to controller and action
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: { _controller: AcmeBlogBundle:Blog:show }
I want to map the path to external url.
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: "www.example.com/blog"
The requirement is, my current website is in kohana, I am porting it gradually to symfony 2. For my symfony2 app kohana URL are like external urls, I want to configure these urls in routing and use them in standard way,
e.g. in Twig,
<a href="{{ path('blog_show'}}">
Read this blog post.
</a>
So later on when I port my pages to Symfony, I will have to change only routing file so that I could use same blog_show key to refer to url and I wont' have to change all the files where I have used urls.
You can do this by using one of the Symfony framework controllers although I'm not sure how this would work with parameters:
blog_show:
path: /blog/{slug}
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: "http://example.com/blog"
permanent: true
Note that path: /blog/{slug} grabs the slug directly, but path: "http://example.com/blog/{slug}" doesn't work.
Source: http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html
As of Symfony 2.2 this is possible by adding the host constraint to the routes:
routing.yml
user_homepage:
path: /path/to/whatever
host: "sub.domain.ext"
defaults:
_controller: forExampleAnyNamespaceBundle:Controller:action
There's an official blog post on this issue: http://symfony.com/blog/new-in-symfony-2-2-url-host-support-in-the-routing
The router feature of Symfony doesn't work that way...
I suggest you create a Twig extension for this. Read more about this here:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
You could create a function that works very similar to the regular url() function, so you can migrate as easily as possible.
{{ legacyUrl('blog_post', {slug: 'my-blog-post'}) }}
After you migrated the blog to Symfony, all you need to do is create a route called "blog_post" and change "legacyUrl" to "url".
Seems there is no native Symfony way to handle this problem.

Categories