Laravel 路由组
Posted
技术标签:
【中文标题】Laravel 路由组【英文标题】:Laravel routing groups 【发布时间】:2016-04-04 20:49:15 【问题描述】:我有一个关于路由组的问题。我有两种类型的用户,我不能使用角色系统。在 laracast 电子邮件验证视频之后,我能够让一种新型用户工作。所以我可以登录并注册没有问题。但是,当我有两种类型的用户路由时,它开始拒绝登录等。
我什至尝试分离管理员用户路由并将艺术家用户路由放在不同的 php 文档中,但仍然不允许两种类型的登录或查看正确的仪表板。
我曾尝试在组内使用命名空间、前缀和中间件,但均无济于事。
这是路由代码。
Route::group(['middleware' => ['web']], function ()
Route::get('/', function ()
return view('welcome');
);
Route::get('/artist', function ()
return view('artist');
);
Route::get('/sponsor', function ()
return view('sponsor');
);
Route::get('/viewer', function ()
return view('viewer');
);
Route::get('/contact', function ()
return view('contact');
);
// This is for the Artist Linkings
//Route::group(['middleware' => 'artist'], function ()
//Route::auth('artist');
//Route::get('art/dashboard', 'SessionsController@index');
//Route::get('art/dashboard', ['middleware' => 'artist', function()
//return view('art/dashboard');
//]);
//);
//Route::get('art/register', 'RegistrationController@register');
//Route::post('art/register', 'RegistrationController@postRegister');
//Route::get('register/confirm/token', 'RegistrationController@confirmEmail');
//Route::get('art/login', 'SessionsController@login');
//Route::post('login', 'SessionsController@postLogin');
//Route::get('/logout', 'SessionsController@logout');
//Route::get('art/dashboard', 'SessionsController@index');
//);
// Need to add the password stuff ect
// Route::group(['prefix' => 'viewer', 'namespace' => 'Viewer'], function ()
// require app_path('Http/Routes/viewers.php');
// );
// This is for all the Viewer Linkings
Route::get('viewer/register', 'ViewerRegistrationController@register');
Route::post('viewer/register', 'ViewerRegistrationController@postRegister');
Route::get('viewer/register/confirm/token', 'ViewerRegistrationController@confirmEmail');
Route::get('viewer/login', 'ViewerSessionsController@login');
Route::post('login', 'ViewerSessionsController@postLogin');
Route::get('/logout', 'ViewerSessionsController@logout');
Route::get('viewer/dashboard', 'ViewerSessionsController@index');
//);
//]);
【问题讨论】:
【参考方案1】:你可以用这个。
对于您定义的艺术家用户
Route::group(["middleware" => ["auth.artist"], "prefix" => "artist","namespace"=>"Artist"], function()
Route::controller('artist', 'UsersArtistController');
Route::controller('controles', 'controlsArtistController');
);
如果不需要任何身份验证,则供查看器用户使用
Route::group("prefix" => "viewer","namespace"=>"Viewer"], function()
Route::controller('Viewer ', 'UsersViewer Controller');
Route::controller('controles', 'controlsViewerController');
);
【讨论】:
我厌倦了这不起作用:(我对每种类型都有单独的设置。每种类型都有自己的注册、登录和仪表板。【参考方案2】: Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware'=>['auth','admin']], function ()
Route::get('dashboard','DashboardController@index')->name('dashboard');
Route::resource('tag','TagController');
Route::resource('category','CategoryController');
);
Route::group(['middleware'=>['auth']], function()
Route::post('favorite/post/add','FavoriteController@add')->name('post.favorite');
Route::post('review/id/add','ReviewController@review')->name('review');
);
Route::group(['as'=>'user.','prefix'=>'user','namespace'=>'Author','middleware'=>['auth','user']], function ()
Route::get('dashboard','DashboardController@index')->name('dashboard');
Route::resource('post','PostController');
);
【讨论】:
以上是关于Laravel 路由组的主要内容,如果未能解决你的问题,请参考以下文章