0

I have a web-app where every User has his own personalized link

https://app.myapp.com/{uuid}/frontpage

Where uuid indicates User resource loaded from the database.

And now, the idea is to move /{uuid}/ to subdomain, so it should look as follows:

https://{uuid}.myapp.com/frontpage

While I have created wildcard DNS and can extract subdomain in Symfony's Controllers easily, the problem is now how to tell Symfony that uuid should be taken from subdomain now.

In Controllers I have routing defined as follows

/**
 * @Route("/{uuid}/frontpage", name="frontpage")
 * @ParamConverter("user", class="AppBundle:User", converter="converter.user")
 * @Template("FrontPage.html.twig")
 */
public function indexAction(Request $request, User $user)
{

}

I would like to avoid rewriting all Controllers and strip out /{uuid}/ part because I have hundreds of Controllers defined like that.

Is there a way to manage this maybe via Listeners?

Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
undefinedman
  • 620
  • 1
  • 11
  • 25
  • What if you did a global regex find/replace so you converted `"/{uuid}/frontpage"` to `"/frontpage", host="{uuid}.yoursite.com"` ? – Jason Roman Aug 08 '17 at 23:29
  • AFAIK this should be done via RouterListener, but I have to admit, that I didn't dive too deep into this topic yet. For sure, you'll find the usefull info here: https://knpuniversity.com/screencast/symfony-journey/httpkernel-router-listener – Jan Rydrych Aug 08 '17 at 23:58
  • @Jason Roman thanks for the advice. It works, however this requires a bit more boilerplate work in annotations, because `yoursite.com` must be dynamic too due to development servers, so I am looking for more sophisticated solution – undefinedman Aug 09 '17 at 08:55
  • @Jan Rydrych I've seen already this link however I will go deeper into RouteListener indeed, thanks! – undefinedman Aug 09 '17 at 08:56

1 Answers1

0

With Symfony 3, you can match a route based on the host. And basically you can also "placeholderize" your subdomain and get the related value in your HttpFoundation\Request object (in your controller for instance).

Ex. :

mobile_homepage:
path:     /
host:     "{subdomain}.example.com"
defaults:
    _controller: AppBundle:Main:mobileHomepage
    subdomain: m

https://symfony.com/doc/current/routing/hostname_pattern.html