Symfony 不检测相对 url 根 [英] Symfony does not detect relative url root

查看:21
本文介绍了Symfony 不检测相对 url 根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 symfony 1.4 的 settings.yml 中我们使用了这个选项:

In symfony 1.4 in settings.yml we are using the option:

env:
  request:
    param: 
      relative_url_root: /name-of-app

指定应用程序使用哪个 relative_url_root.所以每个应用程序都在不同的 relative_url_root 下运行.

to specify which relative_url_root an app uses. So every app runs under a different relative_url_root.

问题是 symfony 1.4 路由没有检测到这个相对的 url 根.例如,如果我们有以下路线:

The problem is that symfony 1.4 routing does not detect this relative url root. So for example if we have the following route:

route_name:
  url:  /some-module/some-action
  param: { module: somemodule, action: someaction }

我们尝试访问 http://myproject.local/name-of-app/some-module/some-action symfony 尝试搜索与 name 匹配的路由名称-of-app/some-module/some-action 而不是 /some-module/some-action.

And we try to access http://myproject.local/name-of-app/some-module/some-action symfony tries to search for a route name which matches name-of-app/some-module/some-action instead of /some-module/some-action.

此外,symfony 现在尝试使用这个 relative_url_root 链接到图像,因此不再找到图像.因为图像只能通过 http://project.local/img/.. 而不是 http://project.local/name-of-app/img

Also symfony now tries link to images using this relative_url_root, therefore images are not found anymore. Because images are only reachable via http://project.local/img/.. instead of http://project.local/name-of-app/img

解决这个问题的最佳方法是什么?

What is the best way to solve this?

  1. 我应该删除某处的/"吗?
  2. 我应该在 htaccess 中以某种方式使用 RewriteBase 吗?
  3. 我应该使用 htaccess 在 REQUEST_URI 中删除应用程序名称"吗?
  4. 为每个路由 URL 加上应用名称的前缀/?
  5. 我应该以某种方式告诉 apache/name-of-app 是某种路径或前缀吗?请注意,在我们的生产环境中,我们无法创建虚拟主机,因此我们必须使用 .htaccess 或 symfony 本身来解决此问题.
  6. 完全不同的解决方案.

推荐答案

修复路由:

我按如下方式更改了我的 .htaccess.

I changed my .htaccess as follows.

RewriteCond %{HTTP_HOST} myproject.local
RewriteCond %{THE_REQUEST} /name-of-app* [NC]
RewriteRule ^(.*)$ dispatcher.php/$1 [QSA,L]

通过使用这个重写规则,symfony 似乎认识到/name-of-app 是路由中的一个前缀,并以某种方式在 relative_url_root 上匹配它.现在,当我在控制器中对 sfRoute 对象进行 var_dump 时,我得到以下信息.

By using this rewrite rule symfony seems to recognize that /name-of-app is a prefix in the routing and somehow matches this on the relative_url_root. Now when I var_dump the sfRoute object in the controller I get the following.

object(sfRoute)[33]
  protected 'isBound' => boolean true
  protected 'context' => 
    array (size=7)
      'path_info' => string 'some-module/some-action' (length=10)
      'prefix' => string '/name-of-appp' (length=8)
      'method' => string 'GET' (length=3)
      'format' => null
      'host' => string 'myproject.local' (length=20)
      'is_secure' => boolean true
      'request_uri' => string 'https://myproject.local/name-of-app/some-module/some-action' (length=47)

请注意,我没有像 Michal 建议的那样更改路由规则.此外,我相信我使用 relative_url_root 的方法并不像 Symfony 理解的那样奇怪.

Note that I did not change the routing rules as Michal suggested. Also I believe my approach with the relative_url_root is not really that strange as Symfony understands it.

修复直接文件访问:

为了将像 https://myproject.local/name-of-app/some/path/to/a/file.ext 这样的请求重定向到 https://myproject.local/some/path/to/a/file.ext 我使用了以下指令.

In order to redirect a request like https://myproject.local/name-of-app/some/path/to/a/file.ext to https://myproject.local/some/path/to/a/file.ext I used the following directives.

RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} ^/name-of-app
RewriteRule ^name-of-app/(.*)$ https://myproject.local/$1 [L]

这篇关于Symfony 不检测相对 url 根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆