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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法