php Symfony2 开发环境有效,生产环境给出 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11862468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:17:47  来源:igfitidea点击:

Symfony2 dev environment works, prod environment gives 404 error

phpsymfony

提问by Homunculus Reticulli

I have recently successfully installed Symfony2 on my machine.

我最近在我的机器上成功安装了 Symfony2。

I can access http:/localhost/app_dev.php(dev environment)

我可以访问http:/localhost/app_dev.php(开发环境)

However, when I try to access the prod environment:

但是,当我尝试访问 prod 环境时:

http:/localhost/app.php

http:/localhost/app.php

I get the following error message in the browser:

我在浏览器中收到以下错误消息:

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 any inconvenience caused.

哎呀!发生错误

服务器返回“404 Not Found”。东西坏了。请通过 [email] 给我们发送电子邮件,让我们知道发生此错误时您正在做什么。我们会尽快修复它。给您造成的任何不便,请原谅。

I have checked the obvious: The file app.php does exist in the same folder as app_dev.php - so I don't know whats causing this.

我已经检查了明显的:文件 app.php 确实与 app_dev.php 存在于同一文件夹中 - 所以我不知道是什么导致了这个。

Does anyone have a solution to fix this?

有没有人有解决方案来解决这个问题?

[[Edit]]

[[编辑]]

I have cleared the cache by typing: sudo php app/console cache:clear env=prod no-debugas recommended. I now get a blank screen. Worryingly, there are no error messages logged in app/logs/prod.log, so I have not got the faintest idea as to what is wrong (prod environment still works just fine).

我已经通过键入:sudo php app/console cache:clear env=prod no-debug按照建议清除了缓存。我现在得到一个空白屏幕。令人担忧的是,app/logs/prod.log 中没有记录错误消息,所以我对出了什么问题一无所知(prod 环境仍然可以正常工作)。

Contents of my app/config/routing.yml file:

我的 app/config/routing.yml 文件的内容:

### fos routing, remove later
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

###


# Internal routing configuration to handle ESI
#_internal:
#   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
#   prefix:   /_internal

Here is my app/config/routing_dev.yml file

这是我的 app/config/routing_dev.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

_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

I just noticed that I DO NOThave a routing_prod.yml**

我只是注意到我没有routing_prod.yml**

(alarm bells ringing) - does Symfony2 not ship with a production routing config file?

(警铃响起)- Symfony2 不附带生产路由配置文件吗?

My Apache configuration file content is shown below:

我的Apache配置文件内容如下所示:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /path/to/symfony/web
    ServerName localhost

    # Custom log file
    Loglevel warn
    ErrorLog  /path/localhost.error.log
    CustomLog /path/localhost.access.log combined

    <Directory /path/to/symfony/web>
        AllowOverride None
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^(.*)$ app.php [QSA,L]
    </Directory>
</VirtualHost>

[[Further Details]]

[[更多细节]]

Contents of app/logs/prod.log

app/logs/prod.log 的内容

[2012-08-10 18:10:38] security.INFO: Populated SecurityContext with an anonymous Token [] [] [2012-08-10 18:10:38] request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /" (uncaught exception) at /path/to/symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php line 83 [] []

[2012-08-10 18:10:38] security.INFO:使用匿名令牌填充 SecurityContext [] [] [2012-08-10 18:10:38] request.ERROR: Symfony\Component\HttpKernel\Exception\ NotFoundHttpException: 在 /path/to/symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php line 83 [] []

回答by tolgap

Have you turned on the production environment and cleared the cache? Run the console and do this:

你有没有开启生产环境并清除缓存?运行控制台并执行以下操作:

app/console --env=prod cache:clear

回答by John Braxler

I think I had a similar problem. I believe warming up the cache solved my instance if it.

我想我有类似的问题。我相信预热缓存解决了我的实例。

php app/console cache:warmup --env=prod --no-debug

回答by Mark Fox

Having recently installed Symfony 2.2 I ran into this issue too but it's actually the normal behavior. Meaning out of the box Symfony 2.x (as of this writing) doesn't ship with any routes/content for the production environment.

最近安装了 Symfony 2.2 我也遇到了这个问题,但这实际上是正常行为。意思是开箱即用的 Symfony 2.x(在撰写本文时)不附带任何用于生产环境的路由/内容。

In your case it looks you installed the Friends of Symfony bundle, which did setup some routes in your production route (routing.yml), but at a glance none of the routes seem to be targeting the root of your production environment i.e. http:/localhost/app.php/so the 404 is probably expected. I can't be totallysure though since it's importing routes so the details are obscured. A great way to check your routes is to read up on Visualizing & Debugging Routeswhere you can learn about the app/console router:debugCLI command.

在您的情况下,您似乎安装了 Friends of Symfony 捆绑包,它确实在您的生产路线 ( routing.yml) 中设置了一些路线,但乍一看似乎没有任何路线针对您的生产环境的根目录,即http:/localhost/app.php/404 可能是预期的. 我不能完全确定,因为它正在导入路线,所以细节被掩盖了。检查路由的一个好方法是阅读可视化和调试路由,您可以在其中了解app/console router:debugCLI 命令。

routing.ymlis the default place for routes in your production environment (meaning routing_prod.ymlisn't a thing). You'll notice routing_dev.ymlimports routing.yml. That means that everything that you put in production is (out of the box) accesible in the development. The demo content you see is exclusive to the dev environment so that's why you're not seeing it in the production. Feel free to move some stuff around to suite your needs but it's generally a good idea for the dev to import the production but not vice versa.

routing.yml是生产环境中路由的默认位置(意思routing_prod.yml不是一回事)。你会注意到routing_dev.ymlimport routing.yml。这意味着您投入生产的所有内容(开箱即用)在开发中都是可访问的。您看到的演示内容是开发环境独有的,因此您在生产环境中看不到它。随意移动一些东西以满足您的需求,但对于开发人员来说,导入产品通常是个好主意,反之亦然。

Any time you want to test out the changes in your production you'll want to clear your cache as @tolgap recommends. The production environment leans heavily on the precompiled cache, so that command will force your production cache to refresh. The dev environment is always refreshing it's cache. Understanding this is crucial to working with Symfony. Though the Book on Symfony.org is a great entry point it doesn't exactly drive home this point about the subtitles of caching and workflow. I was definitely confused for a minute before I realized the relationship between routing.ymland routing_dev.ymland the soft caching of the dev versus the hard caching of the production.

任何时候您想测试生产中的更改时,您都需要按照@tolgap 的建议清除缓存。生产环境严重依赖预编译缓存,因此该命令将强制刷新您的生产缓存。开发环境总是刷新它的缓存。理解这一点对于使用 Symfony 至关重要。尽管 Symfony.org 上的 Book 是一个很好的切入点,但它并没有完全说明缓存和工作流的副标题。我肯定是糊涂了一会儿我才意识到之间的关系routing.yml,并routing_dev.yml与开发与生产的硬盘缓存的软缓存。

回答by Jean-Luc Barat

I tried the solution @gilden gave in his comment; it works.

我尝试了@gilden 在他的评论中给出的解决方案;有用。

I ran php bin/console cache:clear --env=prodinstead of rm -rf app/cache/*.

我跑php bin/console cache:clear --env=prod而不是rm -rf app/cache/*.

回答by Francisco Olmedo Valencia

Today I had the same problem and tried various solutions. Until I remembered that it is important to have the mod_rewrite apache service enabled. To do this run the following command (on Ubuntu)

今天我遇到了同样的问题并尝试了各种解决方案。直到我想起启用 mod_rewrite apache 服务很重要。为此,请运行以下命令(在 Ubuntu 上)

sudo a2enmod rewrite

And then restart the server

然后重启服务器

sudo service apache2 restart

I hope that someone can be helpful. Regards!

我希望有人可以提供帮助。问候!

回答by Eagle1

I'm surprised that nobody told you: you have no route for / on the prod environment.

我很惊讶没有人告诉你:你没有 / 在 prod 环境中的路线。

write this in app/config/routing.yml

在 app/config/routing.yml 中写这个

  _welcome:
 pattern:  /
 defaults: { _controller: AcmeDemoBundle:Welcome:index }

回答by user2679374

You need to install APC for your local server, and everything will work in prod mod for you. Also try to hit correct database in parameters.yml, because dev is adding DATABASENAME_dev.

您需要为本地服务器安装 APC,一切都将在 prod mod 中为您工作。还要尝试在 parameters.yml 中命中正确的数据库,因为 dev 正在添加 DATABASENAME_dev。

回答by Moh .S

My symfony cache folder was not writable. It worked when i made the cache complete directory and its files as writable.

我的 symfony 缓存文件夹不可写。当我将缓存完整目录及其文件设为可写时,它就起作用了。

sudo chmod -R 777 .

回答by Jose Olivares

(Agrega esto dentro del archivo routing_dev.yml) Add this in routing_dev.yml

(Agrega esto dentro del archivo routing_dev.yml) 在routing_dev.yml 中添加这个

_wellcome:
    pattern: /
    defaults: { _controller:AppBundle:Default:index }