Symfony2创建第一个控制器但是出现404错误。

6
我已经将Symfony2 2.7安装在C:\xampp\htdocs\sym1\blog目录下,我手动创建了一个新的控制器,按照这个 文档 的说明进行操作。
   <?php
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class LuckyController extends Controller
{
    /**
     * @Route("/lucky/number")
     */
    public function numberAction()
    {
        $number = rand(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

但是当我访问

http://localhost/sym1/blog/web/lucky/number

或者

http://localhost/sym1/blog/app_dev.php/lucky/number

时,它只会显示

Oops! An Error Occurred

The server returned a "404 Not Found".

Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

有人知道问题是什么吗?

--更新--

我刚刚发现了这条评论

 #RewriteRule .? %{ENV:BASE}/app.php [L]

然后添加这两行代码。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]

会好的,但页面底部会有一个性能条。


1
每次想在生产环境中看到更改时,请运行 php app/console cache:clear --env=prod - malcolm
2个回答

7

我认为,您不理解Symfony2中环境的概念。在第一种情况下,Apache从web文件夹执行了app.php。这是您应用程序的生产版本。有许多缓存文件,这些文件不会在每个请求时刷新。这就是您看不到更改的原因。您必须先通过控制台命令清除缓存。

php app/console cache:clear --env=prod

在第二种情况下,Apache会执行app_dev.php。这是开发环境。您可以立即看到更改,并且还可以查看非常有用的开发工具栏。 工具栏仅存在于开发环境中。 http://symfony.com/doc/current/book/configuration.html#environments

1
另一个可能的解决方案:

php bin/console cache:clear --env=prod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接