2015-07-13 52 views
0

我使用Slim框架作爲背景來調用API。如何在php Slim框架中添加參數?

我已經的ConfigEd composer.json文件:

{ 
    "require": { 
    "slim/slim": "^2.6", 
    "slim/middleware": "*" 
    }, 
    "autoload": { 
     "psr-4": {"App\\Controller\\": "server/controllers/"} 
    } 
} 

而且index.php文件:

<?php 
require 'vendor/autoload.php'; 
$configs = array( 'mode'=>'development', 
       'debug'=>true, 
       'log.enabled' => true, 
       'templates.path' => './templates', 
       'cookies.secret_key' => 'secret' 
      ); 
$app = new \Slim\Slim($configs); 

$app->get('/login', function() use ($app) { 
    $app->render('login.php'); 
}); 

$app->post('/login', function() use ($app) { 
    $process= new App\Controller\MiddleProcess(); 
    // process request to call API 
    $process->transferRequest(); 
}); 

$app->run(); 
?> 

我在目錄server/controllers/有一個文件MiddleProcess.php

<?php 
namespace App\Controller; 
class MiddleProcess { 
public $url = 'http://api.com/'; 
function transferRequest() { 
    $app = \Slim\Slim::getInstance(); 
    $post_datas = $app->request->post(); 
    $token = $app->getCookie('token'); 
    $url = $this->url.'/login'; 
    // How can I add param: `$token` and change 'url', then continue to call API here 
    // example: http://api.com/login?access_token=$token 

} 
} 
?> 

如何改變url請求nd添加參數access_token。然後繼續調用API?

+0

如果你想簡單地重定向,我的答案在下面就足夠了。 – r3wt

回答

0

使用slim的redirect方法。

//first merge any existing get params with our token param and build the query string. 
$query_string = http_build_query(array_merge($app->request->get(),['access_token'=>$token])); 
//call app redirect 
$app->redirect($url.'?'.$query_string)); 
+0

謝謝@ r3wt。我明白你的解決方案是'重定向'。但是'POST'參數怎麼樣:'username','password'? – datnq

+0

@datnq閱讀[this](http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect)。 –

+0

@DavidePastore:謝謝 – datnq