繁体   English   中英

GET参数不适用于Restler框架

[英]GET parameters not working with Restler framework

我很难让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参数。

例如,数学示例不起作用。

如果我呼叫/ 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,
);

$ _SERVER在index.php上的变量

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
)

解决了这个问题:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM