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.
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.
I'm (almost) new to Symfony and I'm using 2.4 but I got a problem that is giving me lots of headaches. For several days I have not been able to fix this issue.
I use the app/console commands to build my base code; from entities to crud:
doctrine:generate:entity (to build the models), then code relations, etc
doctrine:generate: entities (to generate setters, getters, etc)
doctrine:schema:update --force (to update to the database all the models)
generate:doctrine:crud (to make controllers, forms, etc....)
At last, since I choose to declare the routing via annotations, I import into my bundle's routing.yml file all the controller routes like:
AutocondatECRBundle_controllers:
resource: "#AutocondatECRBundle/Controller"
type: annotation
This, as far as I'm aware, makes available all the routes inside the generated controllers at crud generation. However, no matter what route I try to test, Symfony keeps telling me:
FileLoaderLoadException: Cannot import resource "/var/www/autocondat-ecr/src/Autocondat/ECRBundle/Controller" from "/var/www/autocondat-ecr/src/Autocondat/ECRBundle/Resources/config/routing.yml". (Class Autocondat\ECRBundle\Controller\Clasificacion_EstudioController does not exist)
no mater what controller or route I choose to test, always fails to find the controller class, and believe me; those classes are there.
-There are no typos on names or cases
-Classes are there, controllers are there
-Routes are there and can't be loaded even using :
pattern: /whatever
defaults: { _controller: AutocondatECRBundle:ControllerWhatever:index }
This is driving me crazy.
For anyone able to help me, here is the source of all the project:
Link to the project
NOTES:
-There are several bundles inside my project; the one I'm testing is AutocondatECRBundle.
-Security inside security.yml file has been deactivated in order to test it faster.
-Of course, database can be generated fast with the same console commands.
Thanks -A LOT- for your help!
For your class Autocondat\ECRBundle\Controller\Clasificacion_EstudioController
Symfony will resolve both the namespace seperator \ and the underscore _ to a directory seperator, using PSR-0 autoloading standards for the autoloading of classes.
That means it is expecting your class to be located in the file src/Autocondat/ECRBundle/Controller/Clasificacion/EstudioController.php.
Name you class to ClasificacionEstudioController and the file to ClasificacionEstudioController.php.
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.
In Symfony2, when accessing my application locally via app_dev.php, everything works fine. However, when I access app.php it 404s:
Oops! An Error Occurred
The server returned a "404 Not Found".
Something is broken. Please e-mail us at [email] and let us know what
you were doing when this error occurred. We will fix it as soon as
possible. Sorry for
A fresh symfony 2 install does not contain any routing for the production environment.
If you take a look under app/config/routing_dev.yml, you will notice that all of the routes that you see in the demo application are defined only for development. If you wish to test the demo on app.php, you have to first copy the routing from routing_dev.yml to routing.yml, and also enable the AcmeDemoBundle under you AppKernel.php:
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
+ new Acme\DemoBundle\AcmeDemoBundle()
}
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
- $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
(+ is the line you should add, - is the line you should remove)
I had the same problem and I just cleared the cache. php app/console cache:clear --env=prod This has solved my problem.
Do not put the attribute to true: $ kernel = new AppKernel ('prod', TRUE); it will activate the debug mode and it is not recommended for the prod.
Had the same problem.
actually there might be several issues. but you must clear the cache with the console command as symfony caches routes, templates and config.
This is normal Symfony caching doing its work. Any changes you make will be live to see/test in app_dev.php (develop environment), but not in app.php(production environment) since it caches everything.
I follow a simple rule that works. Every time I update anything in app\config\routing.yml (or any change really) and i want to see it in production, you have to CLEAR THE CACHE by running the following console command:
Symfony 2.*: php app/console cache:clear --env=prod
Symfony 3.*: php bin/console cache:clear --env=prod
Now try reloading the page on your browser, and you will see it work.
Okay, I've had the same problem and clearing cache did not resolve it. After an hour of reading posts where everyone says "clear cache" I've decided to really understand whats going on. So I'll try to explain to other people like me (who just have started). Hope I'm not wrong and if so, correct me please.
I'll assume that you're following the book's tutorial, where you have Acme/DemoBundle. And accessing it from production environment gives you 404. First of all, you should have a clear understanding of what bundle in Symfony means. It's something like a plugin. It's like a puzzle. Symfony is like a big puzzle and your app is a piece of the puzzle.
So first, lets look in the AppKernel.php file in ./app. What we see there is registering bundles. Like putting the pieces of the puzzle. And first we say "okay, i want the main pieces of the puzzle, the Symfony bundles" and then we say "and if I'm in debug mode, I also want some other pieces." And there is your piece, your bundle. So that's why you can't access it from production environment. You only register the bundle from developer environment. Register your bundle (Acme/DemoBundle/AcmeDemoBundle) in the top, and you can access it from production environment.
Second, go into ./app/config/routing_dev.yml . This is the routing for development environment. We're saying "okay, I have some routing information in #AcmeDemoBundle/Resources/config/routing.yml and in development environment, our bundle is found. But look in ./app/config/routing.yml .We don't mention anything about our custom routing. It's like the Framework doesn't know about the existence of our routing file. And this is in the production environment. So adding the last part of routing_dev.yml to routing.yml (in ./app/config/) should fix the problem.
After that clear the cache and check if /app.php/random/[number] works. It should be.
I hope this will help someone like me, understanding some of the basics.
When you follow the answer of Anton, and still get the error, you can try the following way
At routing.yml, add this lines (only)
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
resource: "#AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
_demo:
resource: "#AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
don't add this lines
_assetic:
resource: .
type: assetic
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
This is an example of the routing.yml that I wrote
# Internal routing configuration to handle ESI
#_internal:
# resource: "#FrameworkBundle/Resources/config/routing/internal.xml"
# prefix: /_internal
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
resource: "#AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
_demo:
resource: "#AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
I must agree with Andrew.
Turning the second AppKernel to TRUE just allows clearer message of debug (and you may notice the application is not faster than expected).
In my case, it told me I had no _welcome route available for production (i.e. routing.yml).
I had to add following lines as mentionned by Misbah and follow other common procedures to get my application running full speed.
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
Looks like you don't have setup the routing correctly.
Check your routing.yml file if it contains a default route for /. If not, add one to the controller/action you want to run.
Well, I found a simpler and faster answer:
first:$kernel = new AppKernel('prod', TRUE);
On the app.php file.
Then,on your routing.yml (the app/config/routing one, not your bundle's one)
just remove the default generated code after the declaration of your routing.
_demo:
resource: "#AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
Has to be deleted. After doing so, it is now working without a problem!
change your environment to developmet, for use the routes configurated in routes_dev.yml
$kernel = new AppKernel('dev', true);
Disclaimer: I'm totally new to Symfony.
Coming from other frameworks it seemed weird that you couldn't switch out the environment/debug based on current environment variables (ie path/domain).
So I renamed app.php to app_prod.php and updated app.php to the following:
<?php
if ($_SERVER['HTTP_HOST'] == 'localhost') {
require_once 'app_dev.php';
} else {
require_once 'app_prod.php';
}
So if I'm running the code on my local it will use dev, if I run it anywhere else it will run as production. You can obviously add in any checks you want, check for staging/production/dev file paths instead of host names.
The problem troubles me a lot and here's my solution:
First change the file line 21 like this:
$kernel = new AppKernel('prod', true);
then you may get reporting issues while viewing /app.php
actually I did these changes to avoid the '404 error':
in appKernel.php:
comment
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
add
new Acme\DemoBundle\AcmeDemoBundle()
at the bottom of function registerBundles();
add
# AcmeDemoBundle routes (to be removed)
_acme_demo:
resource: "#AcmeDemoBundle/Resources/config/routing.yml"
to routing.yml
I'm sorry that I don't know how to use code snippet widget well,
But I hope I may help you.