douluan8828 2015-01-19 11:47
浏览 40
已采纳

所有路线指向/

I have a Javascript app that uses Slim (slimframework.com) as the API endpoint.

I have used this for quite a while on my previous computer (win 8), but moved it and now its running on OSX (Apache 2.4.9 php 5.5).

Strange thing is that after this migration, whatever GET routes i try, they all route to / (the default route). when i try to get the path of the request (as you can see in the '/' route) it shows the correct request.

I picked it all apart and set up the most minimal layout. Same results.

php:

require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

$app->get('/', function () use ($app) {
    echo "root" . $app->request()->getPath();
});

$app->run();

Calling /hello/world should return the string "Hello, world". But instead it returns "root /hello/world/" note the trailing slash.. This goes for any other route as well (ie /hello/world/and/other/planets/as/well returns "root/hello/world/and/other/planets/as/well/").

I am wondering wether this has something to do with my virtualhost config (but i have many apps that use url rewriting, and that seems to work)

httpd-vhost.conf

<VirtualHost *:80>
  ServerName stage_api.loc
  ServerAlias stage_api.loc
  DocumentRoot "[the correct dir]"

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) /index.php [L,QSA]

  <Directory "[the correct dir]">
    Options Indexes FollowSymlinks MultiViews
    AllowOverride All
    Require all granted
  </Directory>

</VirtualHost>

Strange thing is that if i omit FollowSymLinks from the vhost config, the whole thing fails.

Something I'm missing here? Any help appreciated.

B

UPDATE: Fearing to be seen as slightly OCD... but I added print_r($_SERVER); to index.php, and all seems correct

Array
(
    [SCRIPT_URL] => /hello/world
    [SCRIPT_URI] => http://stage_api.loc/hello/world
    [HTTP_HOST] => stage_api.loc
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CACHE_CONTROL] => no-cache
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
    [HTTP_ACCEPT] => */*
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, sdch
    [HTTP_ACCEPT_LANGUAGE] => en,en-US;q=0.8,nb;q=0.6
    [PATH] => [removed by user]
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache/2.4.9 (Unix) OpenSSL/0.9.8za PHP/5.5.14
    [SERVER_NAME] => stage_api.loc
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 127.0.0.1
    [DOCUMENT_ROOT] => [removed by user]
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => [removed by user]
    [SERVER_ADMIN] => you@example.com
    [SCRIPT_FILENAME] => [removed by user]/index.php
    [REMOTE_PORT] => [removed by user]
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /hello/world
    [SCRIPT_NAME] => /hello/world
    [PHP_SELF] => /hello/world
    [REQUEST_TIME_FLOAT] => 1421675412.017
    [REQUEST_TIME] => 1421675412
    [argv] => Array
        (
        )

    [argc] => 0
)
  • 写回答

1条回答 默认 最新

  • doubi9255 2015-01-20 06:56
    关注

    So. Using Stackoverflow as a rubberduck I eventually came up with a soulution of sorts (if anyone stumbles across the same issue).

    The environment is misinterpreted in Slim framework when using a rewritten url.

    Had to override setting of a property in Environment.php (line 143)

    from:
      $env['PATH_INFO'] = substr_replace($requestUri, '', 0, strlen($physicalPath));
    to: 
      $env['PATH_INFO'] = $_SERVER['REQUEST_URI'];
    

    This is of course not ideal. But gets me up and running while finding a better way of determining if the url is rewritten or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗