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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services