doumaji6215 2013-12-05 05:41
浏览 39
已采纳

如何使用Slim Framework处理php中的jsonp请求?

when i send a jsonp GET request with jQuery, it usually sends something like:

http://website.com/test?callback=jQuery20309569547907449305_1386221743664&id=9&limit=10&_=1386221743665

in Zend Framework i will handle this like:

$request  = $this->getRequest();
$callback = $request->getParam('callback');
$id       = $request->getParam('id');
$limit    = $request->getParam('limit');

// set $response var to something

$this->getResponse()->setBody($callback . '(' . json_encode($response) . ');');

in Slim Framework i have:

$callback = isset($_GET['callback']) ? $_GET['callback'] : '';
$app->get(
    '/test',
    function () {
        $resp = array('This is a TEST route');
    }
);
$app->response->setBody($callback . '(' . json_encode($resp) . ');');

but the route returns 404

any ideas how can i have this working?

  • 写回答

4条回答 默认 最新

  • doudeng2025 2013-12-05 12:54
    关注

    There are several things wrong here. First, you should not be getting a 404, you should be getting an error complaining that $resp is not defined.

    I think you are probably missing a .htaccess (or web.config if you are on IIS) that is routing all requests to your front controller file (where you define your Slim object and routes). To see if this is the problem, try http://website.com/index.php/test?callback=whatever, where index.php is the name of your front controller file.

    This is the .htaccess that I use:

    RewriteEngine On
    
    #Slim PHP routing
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteRule ^ index.php [QSA,NC,L]
    

    As for trying to achieve what you want to achieve, you need something like this:

    $app = new Slim\Slim();
    
    $app->get('/test', function () use($app) {
    
        //Request processing begins here...
    
        //Get callback from query string
        $callback = $app->request()->get('callback');
    
        //Check for null here...
    
        //Set content type to javascript
        header('Content-type: text/javascript');
    
        //Generate our JSONP output
        echo "$callback(" . json_encode(array('This is a test.')) . ");";
    
        //Request processing ends here...
    
    });
    
    $app->run();
    

    I'm not 100% familiar with Zend, but I think it uses a more traditional MVC implementation where you have a Controller class that you extend and implement actions as methods. Slim is much more basic than that, instead you define routes on your app objects and map these to closures, which are executed when their route is hit.

    In my example above, I define a closure for the route '/test'. Closures in PHP have no access by default to other variables in their scope. In order to access a variable outside of the closure scope we must explicitly specific the variables we want via the "use" keyword. In the example, I "use" the $app object, so that we can use the app object inside our closure. This is the basis for the majority of the functionality Slim provides. The $app object is the IOC object, the core where everything lives and should be used to expose service objects, etc. In this case, we are using the request() method that returns us a wrapper around the request related superglobals ($_GET, $_POST, etc).

    Once we have our callback parameter, we can validate, and then generate and send our JSONP. Slim does not abstract (as far as I know) send data back down the response, you should just use echo as in vanilla PHP. You should also set the header type to javascript since that is what we are sending. Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog