douliao5467
douliao5467
2014-05-09 08:53
浏览 34
已采纳

Laravel - 子域本地化

I want to set the localization using subdomains. I've managed to set up subdomain wildcards and it's working fine. However I'd like to set up filters. For example I was thinking of setting up an array of available countries in the config:

<?php

return array(

    'available' => array(
        'uk',
        'fr',
        'de'
    )

);

Then in my routes I need a way of filtering a group. For the moment my code is the following without any filters:

<?php

$homeController = 'MembersController@profile';

if ( ! Sentry::check())
{
    $homeController = 'HomeController@index';
}

Route::group(['domain' => '{locale}.'.Config::get('app.base_address')], function() use ($homeController)
{
    Route::get('/', ['as' => 'home', 'uses' => $homeController]);

    Route::post('users/register', ['as' => 'register', 'uses' => 'UsersController@register']);
    Route::resource('users', 'UsersController');
});

Does anyone have any ideas for filtering the group?

Also if the subdomain isn't valid how can I redirect to something like uk.domainname.com?

Thank you in advance for any help, it's much appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doujue1246
    doujue1246 2014-05-09 10:33
    已采纳

    you could solve this in your routes with a filter, that will be executed first. it checks then for the available subdomains and if it doesn't find it, it redirects to a default subdomain.

    Route::filter('subdomain', function()
    {
        $subdomain = current(explode('.', Request::url()));
        if (!in_array($subdomain, Config::get('app.countries.available'))) {
            return Redirect::to(Config::get('app.default_subdomain') . '.' . Config::get('app.base_address'));
        }
    });
    
    Route::group(['before' => 'subdomain'], function()
    {
        ...
    }
    
    点赞 评论
  • douzhan1963
    douzhan1963 2014-05-09 10:35

    In your app/filters.php I would write something like this. You will have a to create a new variable in your config called availableSubdomains with your subdomains array.

    <?php
    Route::filter('check_subdomain', function()
    {
        $subdomain = Route::getCurrentRoute()->getParameter('subdomain');
        if (!in_array($subdomain, Config::get('app.availableSubdomains')))
            return Redirect::home();
    });
    

    Then I will add a before filter in your group route in app/routes.php

    <?php
    Route::group(
        ['domain' => '{locale}.'.Config::get('app.base_address'),
        'before' => 'check_subdomain']
    , function() use ($homeController)
    {
        Route::get('/', ['as' => 'home', 'uses' => $homeController]);
    
        Route::post('users/register', ['as' => 'register', 'uses' => 'UsersController@register']);
        Route::resource('users', 'UsersController');
    });
    

    Sorry, I haven't tested it.

    点赞 评论
  • dsk95913
    dsk95913 2015-07-23 19:27

    For subdomains where the language designator is the first part of the subdomain name, like: en.domain.com/section here is what I've used for laravel 5.1

    this uses getHost which avoids the problem with http/https showing up first

    app\Httpoutes.php:

    Route::filter('subdomain', function() {
      $locale_url = current(explode('.', Request::getHost()));
      if (!in_array($locale_url, Config::get('app.countries_available'))) {
      return Redirect::to(Config::get('app.default_subdomain') . '.' . Config::get('app.base_address'));
      }
      App::setLocale($locale_url);
    });
    

    it is necessary to add things like:

     config/app.php:  
         'countries_available' => ['en','es'],
    

    to find the variable

    finally, to add it to a route

    app\Httpoutes.php:

    Route::group(['before' => 'subdomain'], function()
    {
      Route::get('/', 'control@func');  
      ...
    });
    
    点赞 评论

相关推荐