Why is my Symfony2 install 404ing when I access app.php?
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
T开发者_StackOverflow中文版he 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.
精彩评论