title: Symfony 5 How to generate URL with slug?
body: <p>I have an application with slug. All routes will start with the same url.
  URL looks like myapp.com/company_slug/locale/.....</p>
  
  <p>After I have submitted the login form, i have an error in my GuardAuthenticator to getLoginUrl().
  <strong>Some mandatory parameters are missing ("company_slug") to generate a URL for route "app_login".</strong></p>
  
  <p>How i can have a company_slug in all route of my application and how to fix this bug ?</p>
  
  <pre><code>&lt;?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-&gt;entityManager = $entityManager;
          $this-&gt;urlGenerator = $urlGenerator;
          $this-&gt;csrfTokenManager = $csrfTokenManager;
          $this-&gt;passwordEncoder = $passwordEncoder;
      }
  
      [....]
  
      public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
      {
          if ($targetPath = $this-&gt;getTargetPath($request-&gt;getSession(), $providerKey)) {
              return new RedirectResponse($targetPath);
          }
  
          $url = $this-&gt;urlGenerator-&gt;generate('index');
  
          return new RedirectResponse($url);
      }
  
      protected function getLoginUrl()
      {
          return $this-&gt;urlGenerator-&gt;generate('app_login');
      }
  }
  </code></pre>
  
  <p>Controller where i would like to redirect to route "index" (/{company_slug}/{_locale}/index). </p>
  
  <pre><code>&lt;?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-&gt;getUser() instanceof User) {
              return $this-&gt;redirectToRoute('admin_index');
          } elseif ($this-&gt;getUser() instanceof Candidate) {
              return $this-&gt;redirectToRoute('candidate_index');
          } else {
              return $this-&gt;redirectToRoute('app_login');
          }
      }
  }
  </code></pre>
  
  <p>Just in case, i setted an company attribute on kernel event.</p>
  
  <pre><code>&lt;?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-&gt;contextStorage = $contextStorage;
      }
  
      public function onKernelRequest(RequestEvent $event)
      {
          if (!$event-&gt;isMasterRequest()) {
              return;
          }
  
          $companySlug = $event-&gt;getRequest()-&gt;get('company_slug');
  
          if (!$companySlug) {
              return;
          }
  
          $company = $this-&gt;contextStorage-&gt;activateContextBySlug($companySlug);
  
          $event-&gt;getRequest()-&gt;attributes-&gt;set('company', $company);
      }
  
      public static function getSubscribedEvents()
      {
          return [
              'kernel.request' =&gt; 'onKernelRequest',
          ];
      }
  }
  </code></pre>