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 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()
    {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程