【问题标题】:Getting Symfony2 to work on shared hosting using only .htaccess仅使用 .htaccess 让 Symfony2 在共享主机上工作
【发布时间】:2012-02-19 19:02:01
【问题描述】:

我已经搜索了 4 天,但找不到有效的解决方案。 我想让 Symfony2 在不访问命令行或 httpd.conf 的情况下在共享主机上工作(无法设置虚拟主机)。我所能做的就是编辑 .htaccess 文件。在我的网络根目录中,我还有一些其他项目(如论坛)。目录结构为:

public_html
 |-forum
 |-ox
 '-Symfony
    |-app
    |-bin
    <...>

我可以让它在 dev ant prod 环境中运行(路由运行良好),但它不加载任何资产(js、css、图像)。在错误日志中总是一样的:

request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /bundles/acmedemo/images/welcome-demo.gif" (uncaught exception)

如果资产不是从 bundles 加载,而是在 twig 中加载,也会发生同样的情况:

{{ asset('css/main.css') }}

然后就结束了

request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /css/main.css" (uncaught exception)

我在 public_html 中的 .htaccess 是:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# DEV ENVIRONMENT #
RewriteRule ^$ Symfony/web/app_dev.php [QSA]
RewriteRule ^(.*)$ Symfony/web/app_dev.php/$1 [QSA,L]

# PROD ENVIRONMENT #
#RewriteRule ^$ Symfony/web/app.php [QSA]
#RewriteRule ^(.*)$ Symfony/web/app.php/$1 [QSA,L]

有什么建议可以让事情变得正确吗?

【问题讨论】:

    标签: .htaccess mod-rewrite symfony shared-hosting


    【解决方案1】:

    有趣的问题。在挖掘代码后,我找到了以下解决方案。

    使用以下代码在src/Vendor/YourBundle/Templating/Asset 文件夹中创建一个名为PathPackage.php 的类。

    <?php
    
    namespace Vendor\YourBundle\Templating\Asset;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;
    
    class PathPackage extends BasePathPackage
    {
        /**
         * Constructor.
         *
         * @param Request $request The current request
         * @param string  $version The version
         * @param string  $format  The version format
         */
        public function __construct(Request $request, $version = null, $format = null)
        {
            parent::__construct("/Symfony", $version, $format);
        }
    }
    

    然后在您的app/config/config.yml 中添加以下参数。

    parameters:
      // ...
      templating.asset.path_package.class: Vendor\YourBundle\Templating\Asset\PathPackage
    

    现在它将 /Symfony 附加到资产 url 参数。

    总结assettwig函数调用getUrl方法来确定url。由this 类扩展。类的对象在templating.helper.assets 服务创建期间作为参数传递。幸运的是 PathPackage 类是configurable。所以解决方案是可能的:)。

    【讨论】:

    • 如果我只是在 config.yml 中添加一行,则会出现错误:InvalidArgumentException: 没有扩展能够加载“templating.asset.path_package.class”的配置(在 ...app\config\config.yml)。寻找命名空间“template.asset.path_package.class”,找到“framework”、“security”、“twig”、“monolog”、“swiftmailer”、“doctrine”、“assetic”、“sensio_framework_extra”、“jms_security_extra”、 "acme_demo", "web_profiler", "sensio_distribution" 这样的参数不就是在 XML 中配置的吗?如果是这样,我不知道该怎么做:(
    • 您必须在参数部分添加它。编辑了我的评论。
    • 然后我得到 ErrorException: Catchable Fatal Error: Argument 1 passed to Cronus\TplBundle\Templating\Asset\PathPackage::__construct() must be an instance of Symfony\Component\HttpFoundation\Request ,没有给出,在第 21 行的 ...app\AppKernel.php 中调用并在 ...src\Cronus\TplBundle\Templating\Asset\PathPackage.php 第 17 行中定义 我想我会保留所有web/bundles... 中的 CSS 文件和图像并使用 asset('bundles/...') 因为我看到没有简单的方法可以做到这一点,我没有时间(经过一周的搜索)
    【解决方案2】:

    在本地做php app/console assets:install ./web,然后上传你的远程共享主机的web文件夹的内容。

    【讨论】:

    • 如何在 Win7 上运行的 xampp 中做到这一点?谷歌搜索,但没有结果。
    • OK.. 我终于让它工作了,但是当我执行命令时,我得到 Could not open input file: app/console
    • @CRONUS 你在 Symfony 文件夹中吗?
    • 好的。它从包中安装资产,但它仍然与全局使用的资产相同 - 例如来自 \app\Resources\css\。执行命令后,它们不会安装在任何地方。我也找不到任何关于此的文档 - 也许这是不可能的,这些全球使用的 css 文件或图像应该在每个包中?如果是这样,那么它从一开始就对我有用 :) 但我认为如果它在没有来自 app.php/css/... 的 htaccess 的情况下正确加载,那么它也应该可以用于 rewrite。
    • 我刚刚发现如果我只使用像 但是如果我使用 {% stylesheets ... %} 它将不起作用{% endstylesheets %}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    相关资源
    最近更新 更多