skip to Main Content

I want to know what is the best way to implement what want (see below) in Silex.

Here is what I want:

  • all URLs must start with locale (/en/page1, /fr/page1, /en/page2, /fr/page2, ….)
  • when the user hits the home page I will check the browser language and add /en or /fr to home page
  • when adding /en or /fr to home page, is there a way to do that in Silex without redirecting (I think redirecting home page is not a good SEO practice (my knowledge in SEO is very limited so maybe i am wrong))?

Thank you

2

Answers


  1. You can have a look at http://silex.sensiolabs.org/doc/organizing_controllers.html

    Especially :

    $app->mount('/blog', $blog);
    

    So using this I think you can do something like :

    $app->mount('/{_locale}', $site);
    

    I haven’t tried it but I think it would work, please let me know 🙂

    Login or Signup to reply.
  2. To me, this was the simplest and cleanest option for Silex v2.x:

    Install the silex-local package

    composer require pmaxs/silex-locale "^2.0"

    Add the LocalServiceProvidet to your app.php file:

    $app->register(new PmaxsSilexLocaleProviderLocaleServiceProvider(), [
        'locale.locales' => ['en', 'ca', 'es'],
        'locale.default_locale' => 'en',
        'locale.resolve_by_host' => false,
        'locale.exclude_routes' => ['^_'],
    ]);
    
    $app->register(new SilexProviderLocaleServiceProvider(), []);
    

    This way, you won’t have to add anything extra in your endpoints, plus everything is handled by default.

    Look at https://github.com/pmaxs/silex-locale for information with Silex v1.x.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search