Fallback route in Symfony 4 while using annotation routing
Fallback route in Symfony 4 while using annotation routing
I'm currently using routing via controller annotations in a Symfony 4 application.
I am trying to route all requests that don't match an existing annotation (e.g. http://example.com/route-that-isnt-defined) to a specific controller and function (e.g. DefaultController::dynamicPage()
which has logic to find if I should be serving content or triggering a NotFoundHttpException
).
DefaultController::dynamicPage()
NotFoundHttpException
Defining the route for DefaultController::dynamicPage()
as @Route("/param")
precedes and intercepts all other defined routes, making them inaccesible.
DefaultController::dynamicPage()
@Route("/param")
I have tried this solution for Symfony 3, not knowing if it will work but get stuck on what "AppBundle" is supposed to refer to, as it's not something that exists in my project.
Currently, my routes.yaml only has one route in it for the index, as all other named routes are defined via annotations:
index:
path: /
controller: AppControllerDefaultController::index
I am looking for either the proper way to implement the link Symfony 3 solution in Symfony 4, or an alternative method of achieving the routing I want without doing something convoluted like extending the exceptions controller and inserting routing functionality into cases of NotFoundHttpException
.
NotFoundHttpException
AppBundle
App
@ehymel I did try that, but it results in an InvalidArguementException "The "App" (from the _controller value "App:Default:index") does not exist or is not enabled in your kernel!"
– Morgan
Sep 16 '18 at 17:32
2 Answers
2
I haven't moved to sf4 yet, but isn't this problem just related to the order of the routes being evaluated? I.e. you could try by just adding explicit definition to load the DefaultController.php annotations in your routes.yml as the last element? I.e. something like this should do the trick (works in sf2.8 at least):
app_annotations:
resource: '@MyBundle/Controller/'
type: annotation
fallback_annotations:
resource: '@MyBundle/Controller/DefaultController.php'
type: annotation
or if that doesn't work in sf4 (if it loads the controller route annotations with some other logic automatically), another workaround would be to just name this fallback controller so that it will be the last one alphabetically (and thus the routes there should be evaluated the last).
You could try adding a kernel event listener that would handle the kernel.exception
event, and for cases where the exception is a NotFoundHttpException
you'd return your custom response instead of the 404 Not Found page.
kernel.exception
NotFoundHttpException
This could be quite flexible since you can implement any custom logic in such listener.
This would work as a kludge, but it's along the same lines as what I said I did not want to do. I'd rather not have routing logic in the exception handling chain.
– Morgan
Sep 16 '18 at 17:34
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
The sf3 default
AppBundle
is nowApp
in sf4. If you use the solution you posted, maybe just try that simple change?– ehymel
Sep 15 '18 at 23:03