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.
Related
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 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.
So first off TL:DR - Symfony isn't picking up additional routing files in my bundle. Don't know why. Tried doing imports like in config.yml and it's not working either
I have multiple controllers for maintainability of my code. I.e. All site related actions are in a SiteController, all app related actions are in an AppController, etc.
So I figured I'd make routing files to correspond with my controllers. The files are housed in MyBundle/Resources/config. The thing is they are not being picked up when i do a php app/console router:debug. So I thought, well I'll just import them into the routing_mybundle.yml file that symfony generated during the generate:bundle process. So I did the following:
imports:
- { resource: routing_site.yml }
- { resource: routing_app.yml }
I'm getting an error message that says:
routing_mybundle.yml contains unsupported keys for "import": "0", "1". Expected one of: "resource", "type", "prefix", "pattern", "path", "host", "schemes", "methods", "defaults", "requirements", "options".
I realize that it's looking for specific keys, but I'm not sure why it would work in the config.yml but not in a routing.yml file.
If I do the following it works:
imports:
resource: routing_site.yml
Or if I "chain" the imports in the files it works. So by this I mean I import routing_app into routing_site and routing_site into routing_mybundle.
Anyone know how to get the imports tag to work, or how to make it so that symfony will pick up my routing_**.yml files?
Thanks :)
EDIT:
Thanks to forgottenbas for the answer. For those who had the same problem as me (multiple config files in the same bundle) here's what I had to do within the routing.yml file WITHIN myBundle/Resources/config.
My directory structure looks like
MyBundle/
Resources/
config/
routing.yml
routing_site.yml
routing_app.yml
So I had to do the following
SiteController:
resource: routing_site.yml
AppController:
resource: routing_app.yml
Thanks Again
You can import routing files this way
routing.yml
SiteBundle:
resource: "#SiteBundle/Resources/config/routing_site.yml"
AppBundle:
resource: "#AppBundle/Resources/config/routing_app.yml"
FOSUserBundle do it the same.