1
votes

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).

Defining the route for DefaultController::dynamicPage() as @Route("/{param}") precedes and intercepts all other defined routes, making them inaccesible.

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: App\Controller\DefaultController::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.

3
The sf3 default AppBundle is now App in sf4. If you use the solution you posted, maybe just try that simple change? – ehymel
@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

3 Answers

1
votes

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).

1
votes

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.

This could be quite flexible since you can implement any custom logic in such listener.

0
votes

From your comment I "smell" you have some composer packages that are not compatible with the current config of your project. Are you upgrading to SF4 from SF3?

I also had this InvalidArguementException:

"The "App" (from the _controller value "App:Default:index") does not exist 
or is not enabled in your kernel...

Turns out that I have a non supported package easycorp/easyadmin-bundle version ^3.1 which I fixed it by

  1. Removing the vendor folder
  2. Removing the composer.lock file.
  3. Explicitly specify the version my project supports ^2.3 in composer.json
  4. Run composer install... et Voila!