Why is my Symfony2 install 404ing when I access app.php?

45,741

Solution 1

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)

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
45,741
siwymilek
Author by

siwymilek

Dev@O323 React Warrior

Updated on June 12, 2020

Comments

  • siwymilek
    siwymilek almost 4 years

    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