I have a very strange issue, with my Symfony2 setup.
I'm working on a restful webservice and would like to setup routing.
I have a fully working application and woud like to change my routing.yml config.
Working configuration
my_product:
resource: My\Bundle\ProductBundle\Controller\DefaultController
type: rest
prefix: /
When I change that to:
my_product:
resource: "#MyProductBundle/Controller/"
type: rest
prefix: /
I get the following error:
Symfony\Component\Config\Exception\FileLoaderLoadException"
message="Can't find class for controller
"#MyProductBundle/Controller/" in #MyProductBundle/Controller/ (which
is being imported from
"/home/myproduct/domains/example/v5/app/config/routing.yml"). Make
sure the "MyProductBundle" bundle is correctly registered and loaded
in the application kernel class. If the bundle is registered, make
sure the bundle path "#MyProductBundle/Controller/" is not empty.
When I change the type from "rest" to "annotation", the error disappears.
What am I doing wrong? I can't find it out and my searches lead to nothing.
Many thanks in advance!
You can't currently import all of a bundle's controllers at once when using FOSRestBundle. It will be added in FOSRestBundle 2.0. Import your controllers individually like in your first example.
Related
What does the type: configuration in a Symfony routing file control? What are its valid values?
I can't find this configuration field explicitly documented anywhere. It's referenced indirectly in Symfony's routing documentation.
app_directory:
resource: '../legacy/routing/'
type: directory
and seems related to loading in additional routes. However, its behavior (or all its allowed values) doesn't seem to be explicatly defined anywhere. I can make a guess that it somehow tells Symfony how to load the external routes, but I'd love to know
Is my guess correct?
Are there valid values other than directory or annotation?
Is this formally documented anywhere?
Is there a spot in the Symfony internals that would be a good place to start finding these answers for myself?
You can find how the type works in the Symfony documentation, see code below. It controls if the routes should be loaded from the PHP annotations or the YAML or XML files found in that (bundle) directory.
app_file:
# loads routes from the given routing file stored in some bundle
resource: '#AcmeOtherBundle/Resources/config/routing.yaml'
app_annotations:
# loads routes from the PHP annotations of the controllers found in that directory
resource: '../src/Controller/'
type: annotation
app_directory:
# loads routes from the YAML or XML files found in that directory
resource: '../legacy/routing/'
type: directory
app_bundle:
# loads routes from the YAML or XML files found in some bundle directory
resource: '#AppBundle/Resources/config/routing/public/'
type: directory
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.
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.
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).
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
Removing the vendor folder
Removing the composer.lock file.
Explicitly specify the version my project supports ^2.3 in composer.json
Run composer install... et Voila!
I am trying to create application in Symfony 2, to learn as much as I can, as beginner. First AppBundle which was created by default was easy. Second I created using create:bundle "ResultBundle", was a bit rough and I got "ClassNotFoundException" at first, but after some settings it worked again as expected.
Armed with not so deserved confidence, I tried to create third bundle "ClinicBundle" and then everything fell apart. Although I created routing.yml for new bundle, and set it correctly in AppKernel.php (link goes right to new class ClinicBundle.php), and set app/config/routing.yml correctly (same as for previous ResultBundle) and added the bundle to composer.json autoload, same as in previous bundle, I kept getting "ClassNotFoundException" for this ClinicBundle, like it can't be loaded in AppKernel.php though when I point to link in that row:
new ClinicBundle\ClinicBundle()
It goes smoothly to the right file. Every advice I have found online was already done and set correctly. Everything was in place but "ClassNotFoundException" kept on and it just wasn't working. I tried clear:cache also but same error popped up even then. I checked everything and it was all in place. routing files, paths, composer autoload. AppKernel...
Finally, I followed some ill advice from a forum and run some dump-autoload command which "generated autoload files" and everything after is much much worse. Now, I am getting long long FileLoaderLoadException error with many lines and main is this one:
Cannot load resource "#ClinicBundle/Resources/config/routing.yml". Make sure the "ClinicBundle/Resources/config/routing.yml" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "#ClinicBundle/Resources/config/routing.yml" is not empty.
I have no idea what is wrong since bundle is registered in AppKernel and this routing file is not empty, it's like this:
clinic_homepage:
path: /clinic
defaults: { _controller: ClinicBundle:Default:index }
app:
resource: '#ClinicBundle/Controller/'
type: annotation
The same structure as previous ResultBundle which worked before all this happened. Also, the ResultBundle does not work anymore too, if I put it on top of routing.yml file in app/config same error happens but for ResultBundle. This is how app/config/rounting.yml file looks like:
clinic:
resource: "#ClinicBundle/Resources/config/routing.yml"
type: annotation
result:
resource: "#ResultBundle/Resources/config/routing.yml"
type: annotation
app:
resource: '#AppBundle/Controller/'
type: annotation
What happened? Does anyone know what is missing here? How do you add new bundle so that it does not report "ClassNotFound"? Thanks!
Just without type: annotation
clinic:
prefix: /some-prefix
resource: '#ClinicBundle/Resources/config/routing.yml'
Since I am new to Symfony and I couldn't manage to find some useful information in google I decided to write to you.
I've read about the way of loading custom DI alias information from a dependency injector in your bundle and how to create a Configuration class that will expose the alias structure. However I am to some extend confused how I can create a file, for example routing.yml, in my AcmeBundle/Resources/config/ folder and read the data from it. E.g:
some_alias:
resource: "#AcmeBundle/Controller/"
type: annotation
prefix: /
I want to make a bundle with routing, independent from the main configuration files in the app folder.
You can create your bundle routing.yml in your WhateverBundle/Resources/config/routing.yml and the in the app/routing.yml just include your bundle's routes.
mybundleorwhatever:
resource: "#WhateverBundle/Resources/config/routing.xml"
I'm having troubles loading resources with sf2, the scenario is the following:
#app/config/routing_rest.yml
rest_api :
type : rest
resource: "#AppBundle/Resources/config/routing_rest.yml"
In the rounting_rest.yml, for the moment, I just want to load all the controllers in the #AppBundle/UserInterface/Web/Symfony/Controller/ folder, which all will be for rest purposes, and the only dummy way I found so far to make it work was the following:
#src/AppBundle/Resources/config/routing_rest.yml
users :
type: rest
resource: "#AppBundle/UserInterface/Web/Symfony/Controller/UserRestController.php"
name_prefix: api_
You shouldn't put a bundle in a bundle. Either have 2 bundles: AppBundle and UserBundle or put everything in 1 bundle (so you end up with src/AppBundle/Resources/config/routing_rest.yml).
If you don't have plans to use the UserBundle in another project, I would recommend using just one bundle.