【发布时间】:2014-08-03 22:11:15
【问题描述】:
我在本地主机的子目录中安装了 Laravel 4.2。所以,我的 URL 类似于 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']);
我认为这更像是一种 hack 而不是解决方案,所以我不打算将它作为答案发布。如果其他人有更好的解决方案,我很乐意听到。
谢谢
【问题讨论】: