我似乎无法让我的过滤器与我的分组路线一起使用。无论我尝试什么,我总是能够访问路线,当我不想。我对laravel仍然有点新意,我无法弄明白为什么它不会起作用。
这是路线
Route::group(array('prefix' => 'bf4'), function()
{
Route::get('scoreboard', 'HomeController@bf4scoreboard');
Route::get('playerinfo/{id}', 'PlayerController@bf4info')->where('id', '[0-9]+');
Route::get('playersearch', 'PlayerController@searchbf4');
Route::post('playersearch', 'PlayerController@searchbf4');
// Only users with the permission to view the battlefield 4 admin section are allowed
Route::group(array('prefix' => 'admin', 'before' => 'bf4_admin'), function()
{
Route::get('records', 'BattlefieldAdminController@showAdKatRecords');
});
});
这是过滤器
Route::filter('bf4_admin', function()
{
if(!Entrust::can('viewbf4admin'))
{
return Redirect::to('/');
}
});
问题是它总是允许我在不应该查看该页面时查看该页面,因为我没有权限在用户角色上查看它。
我做错了什么?
编辑1
这是完整的路线文件。它很乱,但以后会被清理干净。
App::missing(function($exception)
{
return View::make('error.404');
});
App::error(function(ModelNotFoundException $e)
{
return Response::view('error.404');
});
Route::resource('upload', 'FileController');
Route::group(array('prefix' => 'user'), function()
{
Route::post('/create', 'UserController@store');
Route::get('/login', 'UserController@login');
Route::post('/login', 'UserController@do_login');
Route::get('/confirm/{code}', 'UserController@confirm');
Route::post('/forgot_password', 'UserController@do_forgot_password');
Route::get('/reset_password/{token}', 'UserController@reset_password');
Route::post('/reset_password', 'UserController@do_reset_password');
Route::get('/logout', 'UserController@logout');
});
Route::group(array('before' => 'auth'), function()
{
Route::get('user/profile/{username?}', 'UserController@show_profile');
//Route::get('/profile/{name?}', 'UserController@show_profile');
Route::get('/profile/edit', 'UserController@edit_profile');
});
// Begin API Route
Route::group(array('prefix' => 'api'), function()
{
// General Battlefield Routes
Route::group(array('prefix' => 'battlefield'), function()
{
Route::get('scoreboard/{id}/chat', function($id)
{
$isBF4 = (DB::table('tbl_server')->join('tbl_games', 'tbl_server.GameID', '=', 'tbl_games.GameID')->where('ServerID', $id)->pluck('Name') == 'BF4') ? TRUE : FALSE;
return Response::json(Helper::getServerChatScoreboard($id, $isBF4));
});
Route::post('adminReports', array('before' => 'auth'), function()
{
return Response::json(Helper::getAdminReports());
});
Route::post('playerSearch/{name?}', function($name = FALSE)
{
return Response::json(Helper::searchForPlayer($name));
});
Route::get('playerInfo/{id?}', function($playerid = FALSE)
{
$info = Helper::buildPlayerProfile($playerid);
if(isset($info['status']) && $info['status'] == 'error') return Response::json($info, 404);
return Response::json($info);
})->where('id', '[0-9]+');
});
// Battlefield 3 Specific Routes
Route::group(array('prefix' => 'battlefield/3'), function()
{
Route::get('scoreboard/{id}', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf3Scoreboard;
return $b->initialize($id);
});
Route::post('scoreboard/{id}/admin', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf3Admin;
return $b->initialize($id);
});
Route::get('population', function()
{
$gameid = DB::table('tbl_games')->where('Name', 'BF3')->pluck('GameID');
return Response::json(Helper::fetchServerPopulation($gameid));
});
});
Route::post('bf3/admin_reports', function()
{
return Response::json(array('status' => 'success'));
});
Route::group(array('prefix' => 'battlefield/4'), function()
{
Route::get('scoreboard/{id}', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf4Scoreboard;
return $b->initialize($id);
});
Route::post('scoreboard/{id}/admin', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf4Admin;
return $b->initialize($id);
});
Route::get('premessage', function()
{
return Helper::fetchPreMessages(Input::get('id'));
});
Route::get('population', function()
{
$gameid = DB::table('tbl_games')->where('Name', 'BF4')->pluck('GameID');
return Response::json(Helper::fetchServerPopulation($gameid));
});
});
Route::group(array('prefix' => 'common'), function()
{
Route::post('adminReports', function()
{
return Response::json(Helper::getAdminReports());
});
Route::get('/repofeed', function()
{
return Response::json(Helper::fetchRepoActivity());
});
});
});
// End API Route
// Begin Page Route
Route::get('install', 'SetupController@install');
Route::get('/', function()
{
return Redirect::to('/dashboard');
});
Route::get('dashboard', 'HomeController@index');
Route::group(array('prefix' => 'bf4'), function()
{
Route::get('scoreboard', 'HomeController@bf4scoreboard');
Route::get('playerinfo/{id}', 'PlayerController@bf4info')->where('id', '[0-9]+');
Route::get('playersearch', 'PlayerController@searchbf4');
Route::post('playersearch', 'PlayerController@searchbf4');
// Only users with the permission to view the battlefield 4 admin section are allowed
Route::group(array('prefix' => 'admin', 'before' => 'bf4_admin'), function()
{
Route::get('records', 'BattlefieldAdminController@showAdKatRecords');
});
});
Route::group(array('prefix' => 'bf3'), function()
{
Route::get('scoreboard', 'HomeController@bf3scoreboard');
Route::get('playerinfo/{id}', 'PlayerController@bf3info')->where('id', '[0-9]+');
Route::get('playersearch', 'PlayerController@searchbf3');
Route::post('playersearch', 'PlayerController@searchbf3');
Route::group(array('prefix' => 'admin', 'before' => 'bf3_admin'), function()
{
Route::get('records', 'BattlefieldAdminController@showAdKatRecords');
});
});
Route::when('admin/*', 'site_admin');
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function()
{
Route::get('memberlist', 'AdminController@memberList');
Route::get('user/{id}', 'AdminController@showuser')->where('id', '[0-9]+');
Route::get('user/{id}/edit', 'AdminController@edituser')->where('id', '[0-9]+');
Route::post('user/{id}/edit', 'AdminController@do_edituser')->where('id', '[0-9]+');
});
// End Page Route
答案 0 :(得分:1)
对我来说很好。我将所有内容复制到我的路由文件中并进行了一些更改,只是为了忽略Entrust并在过滤器中显示denied
消息:
点击http://server.dev/site/bf4/admin/records会给我denied
。所以过滤器正在工作,重定向也有效,我只是将其删除以查看明确的消息。
将此全部复制到您的路线文件并尝试相同。如果它不起作用,您可能在其他地方遇到问题:
App::missing(function($exception)
{
return View::make('error.404');
});
App::error(function(ModelNotFoundException $e)
{
return Response::view('error.404');
});
Route::filter('bf4_admin', function()
{
return 'denied';
});
Route::resource('upload', 'FileController');
Route::group(array('prefix' => 'user'), function()
{
Route::post('/create', 'UserController@store');
Route::get('/login', 'UserController@login');
Route::post('/login', 'UserController@do_login');
Route::get('/confirm/{code}', 'UserController@confirm');
Route::post('/forgot_password', 'UserController@do_forgot_password');
Route::get('/reset_password/{token}', 'UserController@reset_password');
Route::post('/reset_password', 'UserController@do_reset_password');
Route::get('/logout', 'UserController@logout');
});
Route::group(array('before' => 'auth'), function()
{
Route::get('user/profile/{username?}', 'UserController@show_profile');
//Route::get('/profile/{name?}', 'UserController@show_profile');
Route::get('/profile/edit', 'UserController@edit_profile');
});
// Begin API Route
Route::group(array('prefix' => 'api'), function()
{
// General Battlefield Routes
Route::group(array('prefix' => 'battlefield'), function()
{
Route::get('scoreboard/{id}/chat', function($id)
{
$isBF4 = (DB::table('tbl_server')->join('tbl_games', 'tbl_server.GameID', '=', 'tbl_games.GameID')->where('ServerID', $id)->pluck('Name') == 'BF4') ? TRUE : FALSE;
return Response::json(Helper::getServerChatScoreboard($id, $isBF4));
});
Route::post('adminReports', array('before' => 'auth'), function()
{
return Response::json(Helper::getAdminReports());
});
Route::post('playerSearch/{name?}', function($name = FALSE)
{
return Response::json(Helper::searchForPlayer($name));
});
Route::get('playerInfo/{id?}', function($playerid = FALSE)
{
$info = Helper::buildPlayerProfile($playerid);
if(isset($info['status']) && $info['status'] == 'error') return Response::json($info, 404);
return Response::json($info);
})->where('id', '[0-9]+');
});
// Battlefield 3 Specific Routes
Route::group(array('prefix' => 'battlefield/3'), function()
{
Route::get('scoreboard/{id}', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf3Scoreboard;
return $b->initialize($id);
});
Route::post('scoreboard/{id}/admin', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf3Admin;
return $b->initialize($id);
});
Route::get('population', function()
{
$gameid = DB::table('tbl_games')->where('Name', 'BF3')->pluck('GameID');
return Response::json(Helper::fetchServerPopulation($gameid));
});
});
Route::post('bf3/admin_reports', function()
{
return Response::json(array('status' => 'success'));
});
Route::group(array('prefix' => 'battlefield/4'), function()
{
Route::get('scoreboard/{id}', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf4Scoreboard;
return $b->initialize($id);
});
Route::post('scoreboard/{id}/admin', function($id = NULL)
{
$b = new App\Models\Battlefield\Bf4Admin;
return $b->initialize($id);
});
Route::get('premessage', function()
{
return Helper::fetchPreMessages(Input::get('id'));
});
Route::get('population', function()
{
$gameid = DB::table('tbl_games')->where('Name', 'BF4')->pluck('GameID');
return Response::json(Helper::fetchServerPopulation($gameid));
});
});
Route::group(array('prefix' => 'common'), function()
{
Route::post('adminReports', function()
{
return Response::json(Helper::getAdminReports());
});
Route::get('/repofeed', function()
{
return Response::json(Helper::fetchRepoActivity());
});
});
});
// End API Route
// Begin Page Route
Route::get('install', 'SetupController@install');
Route::get('/', function()
{
return 'home';
});
Route::get('dashboard', 'HomeController@index');
Route::group(array('prefix' => 'bf4'), function()
{
Route::get('scoreboard', 'HomeController@bf4scoreboard');
Route::get('playerinfo/{id}', 'PlayerController@bf4info')->where('id', '[0-9]+');
Route::get('playersearch', 'PlayerController@searchbf4');
Route::post('playersearch', 'PlayerController@searchbf4');
// Only users with the permission to view the battlefield 4 admin section are allowed
Route::group(array('prefix' => 'admin', 'before' => 'bf4_admin'), function()
{
Route::get('records', 'BattlefieldAdminController@showAdKatRecords');
});
});
Route::group(array('prefix' => 'bf3'), function()
{
Route::get('scoreboard', 'HomeController@bf3scoreboard');
Route::get('playerinfo/{id}', 'PlayerController@bf3info')->where('id', '[0-9]+');
Route::get('playersearch', 'PlayerController@searchbf3');
Route::post('playersearch', 'PlayerController@searchbf3');
Route::group(array('prefix' => 'admin', 'before' => 'bf3_admin'), function()
{
Route::get('records', 'BattlefieldAdminController@showAdKatRecords');
});
});
Route::when('admin/*', 'site_admin');
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function()
{
Route::get('memberlist', 'AdminController@memberList');
Route::get('user/{id}', 'AdminController@showuser')->where('id', '[0-9]+');
Route::get('user/{id}/edit', 'AdminController@edituser')->where('id', '[0-9]+');
Route::post('user/{id}/edit', 'AdminController@do_edituser')->where('id', '[0-9]+');
});
// +--------+----------------------------------------------+----------------------------------------------+---------------------------------------------+------------------+---------------+
// | Domain | URI | Name | Action | Before Filters | After Filters |
// +--------+----------------------------------------------+----------------------------------------------+---------------------------------------------+------------------+---------------+
// | | GET upload | upload.index | FileController@index | | |
// | | GET upload/create | upload.create | FileController@create | | |
// | | POST upload | upload.store | FileController@store | | |
// | | GET upload/{upload} | upload.show | FileController@show | | |
// | | GET upload/{upload}/edit | upload.edit | FileController@edit | | |
// | | PUT upload/{upload} | upload.update | FileController@update | | |
// | | PATCH upload/{upload} | | FileController@update | | |
// | | DELETE upload/{upload} | upload.destroy | FileController@destroy | | |
// | | POST user/create | | UserController@store | | |
// | | GET user/login | | UserController@login | | |
// | | POST user/login | | UserController@do_login | | |
// | | GET user/confirm/{code} | | UserController@confirm | | |
// | | POST user/forgot_password | | UserController@do_forgot_password | | |
// | | GET user/reset_password/{token} | | UserController@reset_password | | |
// | | POST user/reset_password | | UserController@do_reset_password | | |
// | | GET user/logout | | UserController@logout | | |
// | | GET user/profile/{username?} | | UserController@show_profile | auth | |
// | | GET profile/edit | | UserController@edit_profile | auth | |
// | | GET api/battlefield/scoreboard/{id}/chat | | Closure | | |
// | | POST api/battlefield/adminReports | | Closure | auth | |
// | | POST api/battlefield/playerSearch/{name?} | | Closure | | |
// | | GET api/battlefield/playerInfo/{id?} | | Closure | | |
// | | GET api/battlefield/3/scoreboard/{id} | | Closure | | |
// | | POST api/battlefield/3/scoreboard/{id}/admin | | Closure | | |
// | | GET api/battlefield/3/population | | Closure | | |
// | | POST api/bf3/admin_reports | | Closure | | |
// | | GET api/battlefield/4/scoreboard/{id} | | Closure | | |
// | | POST api/battlefield/4/scoreboard/{id}/admin | | Closure | | |
// | | GET api/battlefield/4/premessage | | Closure | | |
// | | GET api/battlefield/4/population | | Closure | | |
// | | POST api/common/adminReports | | Closure | | |
// | | GET api/common/repofeed | | Closure | | |
// | | GET install | | SetupController@install | | |
// | | GET / | | Closure | | |
// | | GET dashboard | | HomeController@index | | |
// | | GET bf4/scoreboard | | HomeController@bf4scoreboard | | |
// | | GET bf4/playerinfo/{id} | | PlayerController@bf4info | | |
// | | GET bf4/playersearch | | PlayerController@searchbf4 | | |
// | | POST bf4/playersearch | | PlayerController@searchbf4 | | |
// | | GET bf4/admin/records | | BattlefieldAdminController@showAdKatRecords | bf4_admin | |
// | | GET bf3/scoreboard | | HomeController@bf3scoreboard | | |
// | | GET bf3/playerinfo/{id} | | PlayerController@bf3info | | |
// | | GET bf3/playersearch | | PlayerController@searchbf3 | | |
// | | POST bf3/playersearch | | PlayerController@searchbf3 | | |
// | | GET bf3/admin/records | | BattlefieldAdminController@showAdKatRecords | bf3_admin | |
// | | GET admin/memberlist | | AdminController@memberList | auth, site_admin | |
// | | GET admin/user/{id} | | AdminController@showuser | auth, site_admin | |
// | | GET admin/user/{id}/edit | | AdminController@edituser | auth, site_admin | |
// | | POST admin/user/{id}/edit | | AdminController@do_edituser | auth, site_admin | |
// +--------+----------------------------------------------+----------------------------------------------+---------------------------------------------+------------------+---------------+