简体   繁体   中英

Symfony2 caching issue

I'm trying to create a "Hello World" with Symfony2 and it seemed to run the first time, but whatever changes I do and save the controller - no changes on the front end whatsoever.

This is how my controller looks like:

namespace Test\CalcBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class CalcController {

    public function indexAction($name) {

        return new Response("<html><body>Hello " . $name . "!</body></html>");

    }

}

No matter how I change this file - nothing reflects in the browser. Is this some kind of caching going on and if so how do I disable it? Browser is not caching it, because if I change the last bit of the url - it reflects on the page.

I'm running this in my development environment - Windows 8, PHP 5.5.3 (XAMPP), Apache.

UPDATE: Sorry, forgot to add, the URL I'm using is this:

http://localhost/test/web/app_dev.php/Calc/name

UPDATE2: app/config/routing.yml:

test_calc:
    resource: "@TestCalcBundle/Resources/config/routing.yml"
    prefix:   /

src/Test/CalcBundle/Resources/config/routing.yml:

test_calc_homepage:
    pattern:  /Calc/{name}
    defaults: { _controller: TestCalcBundle:Default:index }

UPDATE3: The exact version of Symfony2 I'm using is 2.3.5

UPDATE4: Found the reason - somehow DefaultController is being used instead of the one I've created... How do I fix that?

UPDATE5: I managed to solve the problem, although I don't know whether it's a good way to do that. I've changed the src/Test/CalcBundle/Resources/config/routing.yml to look like this:

test_calc_homepage:
    pattern:  /Calc/{name}
    defaults: { _controller: TestCalcBundle:Calc:index }

UPDATE6: Solved - the problem was default controller left in routing.yml due to lack of understanding.

Symfony has two access points to the application in the web dir

  • web/app.php (this is for production the cache is active)
  • web/app_dev.php (this is for dev the cache changes when you change something like templates, etc.)

point your browser to http://localhost/app_dev.php

you can get more information about environments in Creating Pages in Symfony 2

you can check if your files are correct

app.php

<?php

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
/*
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
*/

require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

app_dev.php

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;

// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);

// This check prevents access to debug front controllers that are deployed by accident to production servers.
// Feel free to remove this, extend it, or make something more sophisticated.
if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();

require_once __DIR__.'/../app/AppKernel.php';

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

that are the files are have with sf 2.3.4

the projects with sf 2.3.* need the Debug::enable(); line, check that line exists in your file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM