我正在尝试实现多个控制器来监听一个路由/account。有两个控制器,并且只有一个应该在选择位于用户角色范围内的 URL 上执行。namespace AppBundle\Controller;use AppBundle\Entity\Role;use Symfony\Bundle\FrameworkBundle\Controller\Controller;use Symfony\Component\Routing\Annotation\Route;/** * @Route("/account") */abstract class DashboardController extends Controller{ protected $userRoles; public function __construct() { $this->userRoles = $this->getUser()->getRoles(); } /** * Get all user roles */ public function getRoles() { return $this->userRoles; } /** * Get user account type * * @return Role */ public function getAccountType(): Role { $accountType = new Role(); foreach ($this->userRoles as $role) { if(Role::ROLE_STUDENT == $role->getName()) { $accountType = $role; } else if(Role::ROLE_SCHOOL_REPRESENTATIVE == $role->getName()) { $accountType = $role; } else if(Role::ROLE_EMPLOYER == $role->getName()) { $accountType = $role; } else if(Role::ROLE_ADMIN == $role->getName()) { $accountType = $role; } } return $accountType; }}namespace AppBundle\Controller;class CompanyDashboardController extends DashboardController{ public function __construct() { parent::__construct(); } /** * @Route("/", name="dashboard_company_home", methods={"GET"}) * @return Response */ public function index() { return $this->render('dashboard/company/index.html.twig'); }}namespace AppBundle\Controller;class AdminDashboardController extends DashboardController{ public function __construct() { parent::__construct(); } /** * @Route("/", name="dashboard_admin_home", methods={"GET"}) * @return Response */ public function index() { return $this->render('dashboard/admin/index.html.twig'); }}这就是我到目前为止所得到的。
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报
0/150
提交
取消