1个Laravel应用程序,2个域名&不同的路线组

时间:2014-11-24 05:41:57

标签: laravel laravel-4 laravel-routing

我有两个与几件事有关的项目:

  • 用户表
  • 设计
  • 资产
  • 功能

每个项目都有自己的URL,project1.com和project2.com用于生产环境,还有project1.local(或local.project1.com,我不在乎)和project2.local (或local.project2.com)。

我知道如何将这些网址重定向到我的laravel文件夹,我不知道的是干净安全告诉laravel:

  • project1.com - >使用路线组1
  • project2.com - >使用路线组2
  • project1.local - >使用路线组1
  • project2.local - >使用路线组2

另外,我想管理我的观点:

  • 视图/ PROJECT1 /
  • 视图/项目2 /
  • views / shared /(有些观点对2个项目来说很常见)

如果我必须拨打类似project1.myfile的路线,那就没关系了,但也许我可以拥有包含' project1'在路线组中使用它。

现在我尝试了类似的东西

Route::group([
    "domain" => "project2.{extension}"
], function($extension)
{

    Route::any("/about", array(
        'as' => 'About Test',
        'uses' => 'HomeController@showWelcome')
    );

});

// Same route group for project1

在HomeController @ showWelcome中我有:

echo URL::route('About Test');

这一行显示如下:http://project2.%7Bextension%7D/about,我试图将$ extension传递给路线中的某个功能,但同样的事情发生了。

如何让网址/路由组正常安全地工作?

感谢所有帮助/评论。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以为每个子域创建一个路由组,然后对每个组使用“之前”过滤器。

Route::filter('domain1', function($request)
{
    $uri = Request::path(); //this might be in $request
    $matches = preg_match('^https?:\/\/([a-zA-Z)]+)\./', $uri);
    $subdomain = $matches[1];
    Session::put('domain', $subdomain); //you can use Session::get('domain'); to retrieve this 
    //for your other conditional logic (like your views folders etc.)
    if(!($subdomain == 'domain1')){
        return false;
    }
    //check your matches to see if your subdomain matches, if it doesn't: return false
});

然后在你的路线中:

Route::group(array('before' => 'domain1'), function()
{
    Route::get('/', function()
    {
        //
    });

    Route::get('something/else', 'someMethod@SomeController');
});

冲洗并重复子域2 ......等。