【发布时间】:2015-12-27 22:27:02
【问题描述】:
我正在使用 slim 并创建了组以简化我的路线文件。 但是由于非管理员用户应该未经授权的组,我想重定向该组中的所有路由,而不是像这样一一重定向所有路由:
index.php
$app->group('/Admin', function () use ($app) { // crée un "groupe d'URL"
$app->get('/users', function () use ($app){
if(isset($_SESSION["type"]) && $_SESSION["type"] == "admin") {
$ctrl = new UserController();
$ctrl->listing($app);
}else{
$app->redirect($app->urlFor('indexAdmin'));
}
})
$app->get('/users', function () use ($app){
if(isset($_SESSION["type"]) && $_SESSION["type"] == "admin") {
$ctrl = new UserController();
$ctrl->listing($app);
}else{
$app->redirect($app->urlFor('indexAdmin'));
}
})
如你所见,相同的代码多次出现,是否可以分解它?
【问题讨论】:
-
Middleware 是要走的路。
标签: authentication redirect routes slim