我正在嘗試實現多個控制器來監聽一個路由/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 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消