为什么我的 Symfony2 在访问 app.php 时安装了 404ing?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6997543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my Symfony2 install 404ing when I access app.php?
提问by siwymilek
In Symfony2, when accessing my application locally via app_dev.php, everything works fine. However, when I access app.php it 404s:
在 Symfony2 中,当通过 app_dev.php 在本地访问我的应用程序时,一切正常。但是,当我访问 app.php 时它是 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
哎呀!发生错误
服务器返回“404 Not Found”。
东西坏了。请通过 [email] 给我们发送电子邮件,让我们知道发生此错误时您正在做什么。我们会尽快修复它。对于某事很抱歉
采纳答案by Nemanja Miljkovi?
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
:
全新的 symfony 2 安装不包含任何生产环境的路由。如果您查看下app/config/routing_dev.yml
,您会注意到您在演示应用程序中看到的所有路由都是仅为开发而定义的。如果你想测试的演示app.php
,你必须先路由从复制routing_dev.yml
到routing.yml
,并且还使AcmeDemoBundle
下你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)
(+ 是您应该添加的行,- 是您应该删除的行)
回答by youssman
I had the same problem and I just cleared the cache. php app/console cache:clear --env=prod
This has solved my problem.
我遇到了同样的问题,我刚刚清除了缓存。php app/console cache:clear --env=prod
这已经解决了我的问题。
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.
不要将该属性设置为 true:$ kernel = new AppKernel ('prod', TRUE);
它会激活调试模式,不推荐用于 prod。
回答by Raffael
actually there might be several issues. but you must clear the cache with the console command as symfony caches routes, templates and config.
实际上可能有几个问题。但是您必须使用控制台命令清除缓存,因为 symfony 会缓存路由、模板和配置。
回答by CodeGodie
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.
这是正常的 Symfony 缓存工作。您所做的任何更改都可以在 app_dev.php(开发环境)中实时查看/测试,但不会在 app.php(生产环境)中查看/测试,因为它会缓存所有内容。
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 CACHEby running the following console command:
我遵循一个有效的简单规则。每次我更新任何内容app\config\routing.yml
(或任何更改)并且我想在生产中看到它时,您必须通过运行以下控制台命令来清除缓存:
Symfony 2.*: php app/console cache:clear --env=prod
Symfony 2.*: php app/console cache:clear --env=prod
Symfony 3.*: php bin/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.
现在尝试在浏览器上重新加载页面,您将看到它工作。
回答by hackohackob
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.
我假设您正在学习本书的教程,其中有Acme/DemoBundle
. 从生产环境访问它可以为您提供404
. 首先,您应该清楚地了解Symfony 中的bundle 是什么意思。它有点像插件。这就像一个谜题。Symfony 就像一个大拼图,而您的应用程序就是拼图的一部分。
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.
因此,首先让在看AppKernel.php
文件./app
。我们看到的是注册包。就像把拼图的碎片。首先我们说“好吧,我想要拼图的主要部分,Symfony 包”然后我们说“如果我处于调试模式,我还想要一些其他的部分。” 还有你的作品,你的包。所以这就是你不能从生产环境访问它的原因。您只能从开发人员环境注册捆绑包。Acme/DemoBundle/AcmeDemoBundle
在顶部注册您的包 ( ),您可以从生产环境访问它。
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.
其次,进入./app/config/routing_dev.yml
。这是开发环境的路由。我们说“好吧,我@AcmeDemoBundle/Resources/config/routing.yml
在开发环境中有一些路由信息,我们的包被找到了。但是看看。./app/config/routing.yml
我们没有提到关于我们的自定义路由的任何内容。就像框架不知道存在我们的路由文件。这是在生产环境中。因此添加routing_dev.yml
to routing.yml
(in ./app/config/
)的最后一部分应该可以解决问题。
After that clear the cache and check if /app.php/random/[number]
works. It should be.
之后清除缓存并检查是否/app.php/random/[number]
有效。它应该是。
I hope this will help someone like me, understanding some of the basics.
我希望这会帮助像我这样的人,了解一些基础知识。
回答by Olivier ROMAND
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).
将第二个 AppKernel 设置为 TRUE 只会让调试信息更清晰(您可能会注意到应用程序没有预期的快)。
In my case, it told me I had no _welcome route available for production (i.e. routing.yml).
就我而言,它告诉我没有可用于生产的 _welcome 路由(即 routing.yml)。
I had to add following lines as mentionned by Misbah and follow other common procedures to get my application running full speed.
我必须添加 Misbah 提到的以下几行,并按照其他常见程序使我的应用程序全速运行。
_welcome: pattern: / defaults: { _controller: AcmeDemoBundle:Welcome:index }
回答by Misbah
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)
在routing.yml
,添加此行(仅)
_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
这是routing.yml
我写的一个例子
# 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
回答by F.P
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.
检查您的routing.yml
文件是否包含/
. 如果没有,请在要运行的控制器/操作中添加一个。
回答by donald
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.
好吧,我找到了一个更简单更快的答案:
首先:$kernel = new AppKernel('prod', TRUE);
在 app.php 文件中。
然后,在您的routing.yml(app/config/routing 之一,而不是您的bundle 的那个)上,在您的路由声明之后删除默认生成的代码。
_demo:
resource: "@AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
_demo:
resource: "@AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
Has to be deleted. After doing so, it is now working without a problem!
必须删除。这样做之后,它现在可以正常工作了!
回答by Pablo Merino
change your environment to developmet, for use the routes configurated in routes_dev.yml
将您的环境更改为 developmentmet,以使用在 routes_dev.yml 中配置的路由
$kernel = new AppKernel('dev', true);
$kernel = new AppKernel('dev', true);