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

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

问题描述

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

  ENV:
  请求:
    参数:
      relative_url_root:/名称的应用

要指定relative_url_root一个应用程序使用。所以每一个应用程序在不同的relative_url_root运行。

问题在于symfony的1.4路由不检测这个相对URL根。因此,例如,如果我们有以下途径:

  ROUTE_NAME:
  网址:/一些模块/某些行动
  参数:{模块:somemodule,动作:someaction}

和我们试图访问 HTTP://myproject.local/name-of-app/some-module/some-action symfony中试图寻找一个路由名称它匹配名称的应用/一些模块/一些行动而不是 /一些模块/一些行动

另外的symfony现在尝试连接到使用此relative_url_root图像,所以图像不会再找到。由于图像是通过 HTTP访问只有://project.local/img/ .. 而不是 HTTP://project.local/name-of -app / IMG

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


  1. 我应该删除'/'的地方?

  2. 我应该使用的RewriteBase不知何故htaccess的?

  3. 我应该使用htaccess的剥离'的名称 - 应用程序 - 在REQUEST_URI?

  4. preFIX与名称的应用每路由URL /?

  5. 我应该以某种方式让Apache的/-的应用名称是某种路径或preFIX的?请注意,在我们的生产环境中,我们不能够创建虚拟主机,所以我们必须解决这个使用的.htaccess或symfony的本身。

  6. 一个非常不同的解决方案。


解决方案

修正了路由:

我改变了我的.htaccess如下:

 的RewriteCond%{HTTP_HOST} myproject.local
的RewriteCond%{} THE_REQUEST /名称的应用* [NC]
重写规则^(。*)$ dispatcher.php / $ 1 [QSA,L]

通过使用这种重写规则的symfony似乎意识到/名称的应用是在路由选择的preFIX并以某种方式匹配这个对 relative_url_root 。现在,当我的var_dump在控制器中sfRoute对象,我得到了以下内容。

 对象(sfRoute)[33]
  保护一个isBound'=>布尔值true
  受保护的语境=>
    阵列(大小= 7)
      'PATH_INFO'=>字符串'一些模块/一些行动'(长度= 10)
      'preFIX'=>字符串'/名称的-APPP'(长度= 8)
      '方法'= GT;字符串'GET'(长度= 3)
      '格式'=>空值
      '主机'= GT;字符串'myproject.local'(长度= 20)
      is_secure'=>布尔值true
      'REQUEST_URI'=>字符串的https://myproject.local/name-of-app/some-module/some-action'(长度= 47)

请注意,我并​​没有更改路由规则的Michal建议。此外,我相信我有办法了 relative_url_root 是不是真的那么奇怪,因为Symfony的理解它。

修正了直接文件访问:

为了重定向像 HTTPS请求://myproject.local/name-of-app/some/path/to/a/file.ext HTTPS://myproject.local/some/path/to/a/file.ext 我用下面的指令

 的RewriteCond%{REQUEST_URI} \\ .. + $
的RewriteCond%{REQUEST_URI}!\\。html $进行
的RewriteCond%{REQUEST_URI} ^ /名称的应用
重写规则^名称的应用/(.*)$ HTTPS://myproject.local/$1 [L]

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

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

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

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 }

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.

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. Should I remove a '/' somewhere?
  2. Should I use RewriteBase somehow in htaccess?
  3. Should I strip 'name-of-app' in the REQUEST_URI using htaccess?
  4. Prefix every routing url with name-of-app/?
  5. Should I in someway tell apache that /name-of-app is some kind of path or prefix? Note that in our production environment we are not able to create virtual hosts so we have to solve this using .htaccess or symfony itself.
  6. A very different solution.

解决方案

Fix for the routing:

I changed my .htaccess as follows.

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

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)

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.

Fix for direct file access:

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天全站免登陆