子目录中的Laravel子域路由

时间:2014-06-13 11:03:03

标签: laravel routing subdomain

我在本地主机的子目录中安装了Laravel 4.2。所以,我的网址就像http://account.localhost/projectName

一切正常,直到我尝试使用link_to_route() ...而不是链接到,例如,http://jdoe.localhost/projectName/post/some-slug它链接到http://jdoe.localhost/post/some-slug

我觉得这与我的路线有关......

Route::group(['domain' => '{subdomain}.localhost'], function () {
    Route::get('/', ['as' => 'index', 'uses' => 'AccountsController@index']);
    Route::get('/posts/{slug}', ['as' => 'account.posts', 'uses' => 'PostsController@index']);
});

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@showHome']);

如果我将域名更改为{subdomain}.localhost/projectName,则它不再识别子域名,只是将我发送到主页...


我看到4.2实现了一种方法forceRootUrl(),但不确定如何使用它。所以,我最终选择了这个:

/**
 * Handle routes to dynamic subdomains
 */

Route::group(['domain' => '{subdomain}.' . Config::get('app.url')], function () {
    Route::get('/', ['as' => 'index', 'uses' => 'AccountsController@index']);

    Route::group(['prefix' => dirname($_SERVER['SCRIPT_NAME'])], function () {
        Route::get('/posts/{slug}', ['as' => 'account.post', 'uses' => 'PostsController@index']);
        // related routes will go here
    });
});


/**
 * Main site routes
 */

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@showHome']);

我认为这更像是一个黑客而不是解决方案,所以我不打算将其作为答案发布。如果其他人有更好的解决方案,我很乐意听到它。

由于

1 个答案:

答案 0 :(得分:0)

您可以使用prefix

Route::group(
    ['domain' => '{subdomain}.localhost', 'prefix' => 'projectName'],
    function () {
        Route::get('/posts/{slug}', ['as' => 'account.posts', 'uses' => 'projectName\\PostsController@index']);
});

然后使用namespace之类的:

namespace projectName;

class PostsController extends baseController {
    //...
}