Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch-all route in Symfony 3

I have a catch-all fallback route in Symfony2 that I couldn't get to work in Symfony3. I tried this exact syntax (a verbatim copy of my Symfony2 route) and that didn't work.

fallback:
    path:     /{req}
    defaults: { _controller: MyBundle:Default:catchAll }
    requirements:
        req: ".+"

How can I get this working in Symfony3? (It's literally the only thing holding me back from using Symfony3 and keeping me at v2.8)

like image 552
Yes Barry Avatar asked Nov 29 '22 23:11

Yes Barry


1 Answers

This should help you:

route1:
  path: /{req}
  defaults: { _controller: 'AppBundle:Default:index' }
  requirements:
      req: ".+"

Where, my controller is called "DefaultController", and I have a function called "indexAction()".

Here is my code for the DefaultController:

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
...

I actually did try what you said in my environment, and it didn't work until I had the right controller settings specified.


EDIT:

For this to work, it was necessary to add the parameter Request $request (with the type hint) to the action's method signature.

like image 138
Alvin Bunk Avatar answered Dec 04 '22 09:12

Alvin Bunk