GET参数不适用于Restler框架

时间:2013-10-02 18:47:41

标签: php restler

我很难让Restler与nginx一起正常工作。 首先,我甚至无法让Math.php / add工作(返回404)。

但我以这种方式管理(我见过很多人都在努力):

root         /var/www/test/;

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_pass   unix:/var/run/php5-fpm/socket.socket;
    fastcgi_index index.php;
    include fastcgi_params;
}

location /api { //map /api to public directory
    try_files $uri /api/public/index.php;
}

fastcgi_param

fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_path_info;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
...

注意:cgi.fix_pathinfo = 1

现在一切正常,除了我不能使用?parameter =

接收任何GET参数

例如,Math示例不起作用。

如果我调用/ api / math / add?n1 = 6& n2 = 4则返回2

但乘法示例有效:GET / math / multiply / 4/3返回{“result”:12}

<?php
class Math
{
    /**
     * @param int $n1
     * @param int $n2
     *
     * @return int
     */
    function add($n1 = 1, $n2 = 1)
    {
        return $n1 + $n2;
    }
}

routes.php文件

//==== GET v1/math/add ====

$o['GET']['v1/math/add'] = array (
    'className' => 'Math',
    'path' => 'v1/math',
    'methodName' => 'add',
    'arguments' => 
    array (
        'n1' => 0,
        'n2' => 1,
    ),
    'defaults' => 
    array (
        0 => 1,
        1 => 1,
    ),
    'metadata' => 
    array (
        'description' => '',
        'longDescription' => '',
        'param' => 
        array (
            0 => 
            array (
                'type' => 'int',
                'name' => 'n1',
                'default' => 1,
                'required' => false,
                'from' => 'query',
            ),
            1 => 
            array (
                'type' => 'int',
                'name' => 'n2',
                'default' => 1,
                'required' => false,
                'from' => 'query',
            ),
        ),
        'return' => 
        array (
            'type' => 'int',
            'description' => '',
        ),
        'resourcePath' => 'v1/math/',
    ),
    'accessLevel' => 0,
);
index.php上的

$ _ SERVER变量

Array
(
    [TEMP] => /tmp
    [TMPDIR] => /tmp
    [TMP] => /tmp
    [PATH] => /usr/local/bin:/usr/bin:/bin
    [HOSTNAME] => 
    [USER] => ftp
    [HOME] => /var/www/
    [FCGI_ROLE] => RESPONDER
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [CONTENT_TYPE] => application/x-www-form-urlencoded
    [CONTENT_LENGTH] => 
    [SCRIPT_FILENAME] => /var/www/test/api/public/index.php
    [SCRIPT_NAME] => /api/public/index.php
    [PATH_INFO] => 
    [REQUEST_URI] => /api/math/add?n1=6&n2=4
    [DOCUMENT_URI] => /api/public/index.php
    [DOCUMENT_ROOT] => /var/www/test
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_SOFTWARE] => nginx/1.4.2
    [HTTPS] => 
    [REDIRECT_STATUS] => 200
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CACHE_CONTROL] => no-cache
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
    [HTTP_CONTENT_TYPE] => application/x-www-form-urlencoded
    [HTTP_ACCEPT] => */*
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
    [PHP_SELF] => /api/public/index.php
    [REQUEST_TIME_FLOAT] => 1380738349.0903
    [REQUEST_TIME] => 1380738349
)

1 个答案:

答案 0 :(得分:0)

解决了这个问题:

location /api {
    if (!-f $request_filename) {
        rewrite ^(.*)$ /api/index.php last;
    }

    if (!-d $request_filename) {
        rewrite ^(.*)$ /api/index.php last;
    }
}