-1

我有一个带有 slug 的应用程序。所有路由都以相同的 url 开头。URL 看起来像 myapp.com/company_slug/locale/.....

提交登录表单后,我的 GuardAuthenticator 中有一个错误,用于 getLoginUrl()。 缺少一些强制参数(“company_slug”)来为路由“app_login”生成 URL。

我如何在我的应用程序的所有路径中都有一个 company_slug 以及如何修复这个错误?

<?php

namespace App\Security;

[.....]

/**
 * Class LoginFormAuthenticator
 */
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $entityManager;
    private $urlGenerator;
    private $csrfTokenManager;
    private $passwordEncoder;

    public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->entityManager = $entityManager;
        $this->urlGenerator = $urlGenerator;
        $this->csrfTokenManager = $csrfTokenManager;
        $this->passwordEncoder = $passwordEncoder;
    }

    [....]

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
            return new RedirectResponse($targetPath);
        }

        $url = $this->urlGenerator->generate('index');

        return new RedirectResponse($url);
    }

    protected function getLoginUrl()
    {
        return $this->urlGenerator->generate('app_login');
    }
}

我想重定向到路由“索引”(/{company_slug}/{_locale}/index)的控制器。

<?php

namespace App\Controller;

use App\Entity\Candidate;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * Class DefaultController.
 *
 * @Route("/{company_slug}/{_locale}")
 */
class DefaultController extends AbstractController
{
    /**
     * @Route("/", name="index")
     */
    public function index()
    {
        if ($this->getUser() instanceof User) {
            return $this->redirectToRoute('admin_index');
        } elseif ($this->getUser() instanceof Candidate) {
            return $this->redirectToRoute('candidate_index');
        } else {
            return $this->redirectToRoute('app_login');
        }
    }
}

以防万一,我在内核事件上设置了公司属性。

<?php

namespace App\EventSubscriber;

use App\ContextStorage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
 * Class AgencyContextSubscriber.
 */
class CompanyContextSubscriber implements EventSubscriberInterface
{
    /**
     * @var ContextStorage
     */
    private $contextStorage;

    public function __construct(ContextStorage $contextStorage)
    {
        $this->contextStorage = $contextStorage;
    }

    public function onKernelRequest(RequestEvent $event)
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $companySlug = $event->getRequest()->get('company_slug');

        if (!$companySlug) {
            return;
        }

        $company = $this->contextStorage->activateContextBySlug($companySlug);

        $event->getRequest()->attributes->set('company', $company);
    }

    public static function getSubscribedEvents()
    {
        return [
            'kernel.request' => 'onKernelRequest',
        ];
    }
}
4

1 回答 1

1

您需要在其中传递参数数组,$this->urlGenerator->generate('index'); 它只是尝试生成您的路线,但它发现 company_slug 丢失,因此无法为您生成 url,在您CompanyContextSubscriber设置属性请求时尝试检索它onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey),试试这个:

$this->urlGenerator->generate('index', ['company_slug' => $request->attributes->get('company')]);

我相信您还需要将 _locale 参数传递给上面的数组

于 2020-01-14T20:24:39.297 回答