仅使用 .htaccess 让 Symfony2 在共享主机上工作
我已经搜索了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题。在深入研究代码后,我找到了以下解决方案。
使用以下代码在
src/Vendor/YourBundle/Templated/Asset
文件夹中创建一个名为PathPackage.php
的类。然后在
app/config/config.yml
中添加以下参数。现在它会将
/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
insrc/Vendor/YourBundle/Templating/Asset
folder with following code.Then in your
app/config/config.yml
add the following parameter.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 :).在本地执行
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.