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.
Related
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'
I'm trying to implement a simple login and logout in my symfony app and in the documentation it says I need to create a route to the logout page. And there is a code like this:
# app/config/routing.yml
logout:
path: /logout
I'm trying to paste it into my app/config/routing.yml, so it looks like this:
# app/config/routing.yml
app:
resource: '#AppBundle/Controller/'
type: annotation
logout:
path: /logout
But I get an error
The file "(...)\app/config\routing.yml" does not contain valid YAML
I was searching through the documentation and couldn't find anythig that would help me solve it. I can't really understand how this routing configuration file works and why I get this error.
Suggest when making changes to any yaml file in a development environment, save the yaml first, make the change, then check for error messages. If you get error messages back them out.
Also set up your editor so that it points out things like tabs, spaces, etc... So it's easier to see right away.
I have a problem when I create a new view.
I've created a html.twig (validate.html.twig)
my routing.yml:
ads_fi_auth_validate:
pattern: /validate
defaults: { _controller: AdsFiAuthBundle:Auth:validate }
methods: [POST, GET]
and I created a validateAction
after all this I get a 404 for validate/ GET.
I think I need a command or something like that to tell symfony that I just create a new route to a new view
Thanks a lot guys
At least in Symfony 2.5 you specify the pattern, with the keyword path, not pattern so if you change your code to the following:
ads_fi_auth_validate:
path: /validate
defaults: { _controller: AdsFiAuthBundle:Auth:validate }
methods: [POST, GET]
You should be able to hit your route. That assuming that the bundle and the method exist.
A good way to find out if Symfony is recognizing your route, is by running the console command:
php app/console router:debug will list all available routes. There's no need (nor way) to let Symfony know that you just created a new route. Symfony automatically picks them up.
I just find that I need clear cache, and this solved my problem
php app/console cache:clear -e prod
I'm working in a Symfony2 project, under development enviroment app_dev.php all works fine but when I go to production app.php I get this error at Firebug console:
"NetworkError: 404 Not Found - http://applicationtest.com/css/fmain.css"
And of course CSS and JS isn't working, what I miss?
It seems that you have not dumped assetic resources:
php app/console assetic:dump --env=prod
And then try again...
for example:
//routing.yml
test_root:
path: /css/fmain.css
defaults: { _controller: AcmeBlogBundle:Blog:index }
..
is valid.
That means that the request of that root will call the indexAction of the Blog Controller in my exemple.
thought, are you trying just to include some css file?
is so check the doc: http://symfony.com/fr/doc/current/book/templating.html#liens-vers-des-fichiers
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.