仅使用 .htaccess 让 Symfony2 在共享主机上工作

发布于 2025-01-07 10:39:44 字数 1271 浏览 3 评论 0原文

我已经搜索了 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)

My .htaccess< public_html 中的 /em> 是:

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]

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

I've been searching for this for 4 days now and couldn't find a working solution.
I want to make Symfony2 work on shared hosting without access to command line or httpd.conf (there's no way to set virtual host). All I can do, is just edit .htaccess files. In my web root directory I also have some other projects (like forum). The directory structure is:

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

I can make it work both in dev ant prod environments (routing works well), BUT it doesn't load any assets (js, css, images). In error log there's always the same:

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

Same happens if asset is loaded not from bundles, but also in twig as:

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

Then it ends up with

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

My .htaccess in public_html is:

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]

Any suggestions how to make things right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2025-01-14 10:39:44

有趣的问题。在深入研究代码后,我找到了以下解决方案。

使用以下代码在 src/Vendor/YourBundle/Templated/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 附加到 asset url 参数。

总结 asset twig 函数调用 getUrl方法来确定url。这是由 this 扩展的 类。类的对象在 temptting.helper.assets 服务创建。幸运的是 PathPackage 类是 可配置的。所以解决方案是可能的:)。

Interesting problem. After digging around the code I found following solution.

Create a class named PathPackage.php in src/Vendor/YourBundle/Templating/Asset folder with following code.

<?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);
    }
}

Then in your app/config/config.yml add the following parameter.

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

Now it will append /Symfony to the asset url parameter.

To summarize asset twig function calls getUrl method to determine the url. Which is extended by this class. Object of the class is passed as argument during templating.helper.assets service creation. Luckily PathPackage class is configurable. So solution was possible :).

南风起 2025-01-14 10:39:44

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

Do php app/console assets:install ./web locally, and upload the content of web folder your remote shared hosting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文