同じルートのため、ユーザーの役割に応じて、異なるコントローラを使用する方法?

ニック:

私は1つのルートに耳を傾け、複数のコントローラを実装しようとしています/account

そこ2つのコントローラがあり、一方だけがどこユーザーのロール内の選択が嘘その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');
    }
}

それは私がこれまで持っているものです。

Yivi:

ルートリスナーがセキュリティリスナーよりも高い優先度で実行されるので、あなたは、「ルート」宣言でこれを行うことはできませんどちらも、中に発生するKernelEvents::REQUESTイベントが、ルーティング、ファイアウォールの前に来ます。

コントローラのマッピングへのルートが解決されているときにあなたは、単に別のリスナーを添付して、上のユーザー情報を注入することができない理由である、あなたが持っていない、まだユーザー情報(Requestそれがために、ルート宣言で使用するために利用可能ですので、オブジェクトを表現マッチング例えば、)。

基本的に、あるルート、一方のコントローラ。あなたはこれらのユーザーのためのロジックを発散したい場合、あなたはそれを適用する必要があります後にあなたがコントローラに入ります。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=13659&siteId=1