Symfony does not detect relative url root

不羁岁月 提交于 2020-01-06 07:05:08

问题


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.

回答1:


I'm not sure if the relative_url_root is what you're really after. As the documentation states:

relative_url_root

The relative_url_root option defines the part of the URL before the front controller. Most of the time, this is automatically detected by the framework and does not need to be changed.

So it is part of the URL before index.php or frontend_dev.php, etc.

What you could do, in my opinion, is to write your own Routing class which will extend the sfPatternRouting and have it remove the prefix part of your URLs before trying to match any route. Then use this class for routing in factories.yml.

EDIT: I somehow missed the fact that you operate between different applications.

In general the relative_url_root is definitely not a good idea to separate calls to different apps. The best way, in my opinion, would be to use separate front controllers and Virtual Hosts to have different subdomains. Unfortunately you say you cannot go for this option.

You could then go for either of the solutions:

  1. Fiddle with .htaccess and have it redirect your calls to a proper front controller: myurl.com/path-to-app1/something will call app1.php/something and myurl.com/path-to-app2/something will call app2.php/something.

  2. Change the index.php file. Have it catch the first part of URL (path-to-app/) and based on it's value load the configuration for the proper app (e.g. $configuration = ProjectConfiguration::getApplicationConfiguration('app2', 'prod', false);) You would then have to change your routes to take into consideration that there is this first part of URL (e.g. have all your routes start with a :path-to-action variable which will be passed to functions generating URLs), or use a parameter defined in config:

.

In apps/app1/config/app.yml
path_to_app: app1

In apps/app1/config/routing.yml
route1:
  url: <?php echo sfConfig::get('app_path_to_app') ?>/something
  ...



回答2:


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]


来源:https://stackoverflow.com/questions/14982583/symfony-does-not-detect-relative-url-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!