Failed to load assets when deploying symfony on web server: assets are not imported, failed to load resource 错误404

Failed to load assets when deploying symfony on web server: assets are not imported, failed to load resource error 404

我正在尝试为生产测试部署一个 symfony 应用程序。我遵循许多我将要解释的步骤:

  1. 首先,我安装了CoreSphereConsoleBundle,以便有一个浏览器控制台。在我的本地配置中(windows 7 和 wamp 服务器 apache),当我想使用她时,我会这样写 url C:\wamp\www\myApp\app_web.php\_console。它运作良好。但是在我的 OVH shared 服务器中,当我写 www.mydomain.com/web/_console 时它根本不起作用(我不明白为什么!)。

  2. 然后我按照symfony doc的步骤;例如,我清除了缓存 php app/console cache:clear,还清除了 php app/console cache:clear --env=prod

  3. 的产品环境
  4. 我允许调试模式。

\web\app.php:

<?php
// web/app.php

// …

$kernel = new AppKernel('prod', true); // set the 2nd argument to true
  1. 我检查兼容性服务器:

web/config.php:

/*if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
  '127.0.0.1',
  '::1',
))) {
  header('HTTP/1.0 403 Forbidden');
  exit('This script is only accessible from localhost.');
}*/

所以当我显示 url www.mydomain.com/web/config.php 时,我的配置页面没有错误。 当然,在我允许我自己的 IP adress 并且我也允许 cachelogs 文件夹到 chmod 777 之后。

  1. 我编辑我的 .htaccess 文件。我有两个 htacess 文件,一个在我服务器的根目录中,另一个在 web 文件夹中。

/.htaccess(根服务器)

SetEnv SHORT_OPEN_TAGS 0
SetEnv REGISTER_GLOBALS 0
SetEnv MAGIC_QUOTES 0
SetEnv SESSION_AUTOSTART 0
SetEnv ZEND_OPTIMIZER 1
SetEnv PHP_VER 5_4_11
AddType x-mapp-php5 .php

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/app.php [QSA,L]

/web/.htaccess(网络文件夹):

SetEnv SHORT_OPEN_TAGS 0
SetEnv REGISTER_GLOBALS 0
SetEnv MAGIC_QUOTES 0
SetEnv SESSION_AUTOSTART 0
SetEnv ZEND_OPTIMIZER 1
SetEnv PHP_VER 5_4_11
AddType x-mapp-php5 .php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
  1. 我为框架做了所有我需要的配置(数据库配置,我的服务器,框架配置等)。

一切正常,例如,当我转到主页时,我的应用程序正在部署和显示。但是我的资产 未加载。事实上,我的css filesjs filesimages没有导入

我部署symfony应用程序哪里出错了?

我想我必须在服务器上安装资产,但我无权访问 CoreSphereConsoleBundle console,请注意我没有共享服务器专用服务器。 在主页上,我的浏览器本机控制台 (google chrome) 显示每个脚本、css 文件和其他客户端针的错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

我在我的应用程序中正确导入了我的资产,并且在我的本地配置中它有效:

layout.html.twig:

{% block script %}
  <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
  <script src="{{ path('fos_js_routing_js', {'callback': 'fos.Router.setData'}) }}"></script>
  {% javascripts
    '@MySpaceMyBundle/Resources/public/js/jquery-2.1.3.min.js'
    '@MySpaceMyBundle/Resources/public/js/bootstrap.min.js'
    '@MySpaceMyBundle/Resources/public/js/hinclude.min.js'
    ...
    'http://code.highcharts.com/stock/highstock.js'%}
    <script type="text/javascript" src="{{ asset_url }}"></script>
  {% endjavascripts %}
{% endblock %}

{% block stylesheets %}
  <link rel="stylesheet" href="{{ asset('bundles/MySpaceMyBundle/css/bootstrap-theme.min.css') }}" type="text/css" media="all"/>
  <link rel="stylesheet" href="{{ asset('bundles/MySpaceMyBundle/css/bootstrap.min.css') }}" type="text/css" media="all"/>
  ...
  {# animate CSS #}
  <link rel="stylesheet" href="{{ asset('bundles/MySpaceMyBundle/css/animate.min.css') }}" type="text/css" media="all"/>
  {# dataTables CSS #}
{% endblock %}

我检查了包裹。 在它的安装指南中,他们要求您在 routing_dev.yml

中添加他们的路由
#app/config/routing_dev.yml
_console:
    resource: "@CoreSphereConsoleBundle/Resources/config/routing.yml"
    prefix: /_console

由于您已将应用 ENV 设置为 prod,因此 routing_dev.yml 中的路由在 PROD 中不起作用。

如果你想在你的 PROD ENV 中访问 /_console ,你应该移动他们给 app/config/routing.yml

的路由配置

Bundle 安装还会指导您在 AppKernel 的 dev 部分中注册 bundle。

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
    // ...
    $bundles[] = new CoreSphere\ConsoleBundle\CoreSphereConsoleBundle();
}

你应该把它移到其他包(非开发)注册的地方..

PS :一旦你这样做了,控制台就会工作,但这是一个 严重的安全问题 并且为任何人提供了一种简单的方法来做他们想做的事想要你的应用程序..

这就是为什么您应该将此捆绑包限制在本地使用。如果你真的想在 PRO env 中启用它,我强烈建议你配置安全防火墙以将路由访问限制为仅站点管理员

示例:

#app/config/security.yml    
access_control:
        - { path: ^/_console$, role: ROLE_ADMIN }