简体   繁体   English

Slim Framework 2.0.0无法与GET一起使用-> params()

[英]Slim Framework 2.0.0 Unable to use ->params() with GET

I'm using SLIM 2.0.0 我正在使用SLIM 2.0.0

Is it possible to use ->params() with GET? 是否可以在GET中使用-> params()?

In the example below 在下面的示例中

  • if I call it by POST: curl -d "param1=hello&param2=world" http://localhost/foo it prints: helloworld CORRECT!! 如果我通过POST调用它: curl -d "param1=hello&param2=world" http://localhost/foo它会打印:helloworld正确!
  • if I call it by GET: http://localhost/foo/hello/world it prints: NOTHING!! 如果我通过GET调用它: http://localhost/foo/hello/world它将打印:NOTHING! <- WRONG!! <-错误!

Why? 为什么?

<?php
require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app -> get('/foo/:param1/:param2', 'foo');
$app -> post('/foo', 'foo');
$app -> run();

function foo() {
    $request = \Slim\Slim::getInstance() -> request();
    echo $request -> params('param1');
    echo $request -> params('param2');
}
?>

SOLVED! 解决了! In the documentation page Request Variables - Slim Framework Documentation I read this: 在文档页面“ 请求变量-Slim Framework文档”中,我阅读了以下内容:

An HTTP request may have associated variables (not to be confused with route variables). HTTP请求可能具有关联的变量(请勿与路由变量混淆)。 The GET, POST, or PUT variables sent with the current HTTP request are exposed via the Slim application's request object. 当前HTTP请求发送的GET,POST或PUT变量通过Slim应用程序的request对象公开。

If you want to quickly fetch a request variable value without considering its type, use the request object's params() method: 如果要快速获取请求变量值而不考虑其类型,请使用请求对象的params()方法:

<?php
$req = $app->request();
$paramValue = $req->params('paramName');

The params() method will first search PUT variables, then POST variables, then GET variables. params()方法将首先搜索PUT变量,然后搜索POST变量,然后搜索GET变量。 If no variables are found, null is returned. 如果找不到变量,则返回null。 If you only want to search for a specific type of variable, you can use these methods instead: 如果只想搜索特定类型的变量,则可以改用以下方法:

<?php
// Get request object
$req = $app->request();

//GET variable
$paramValue = $req->get('paramName');

//POST variable
$paramValue = $req->post('paramName');

So: 所以:

The key line is "An HTTP request may have associated variables (not to be confused with route variables)." 关键行是“ HTTP请求可能具有关联的变量(不要与路由变量混淆)”。

http://domain.com/foo/hello/wold?name=brian

In the above URI the route variables/parameters are read from the '/foo/hello/world' portion. 在上面的URI中,路由变量/参数是从“ / foo / hello / world”部分读取的。 The request GET variables are read from the query string ('name=brian') and can be accessed by $app->request()->get('name') or $app->request()->params('name'). 请求GET变量是从查询字符串('name = brian')中读取的,可以通过$ app-> request()-> get('name')或$ app-> request()-> params('名称')。

The request POST variables are parsed from the body of the request and can be accessed $app->request()->post('param1') or $app->request()->params('param1'). 请求POST变量是从请求的正文中解析的,可以通过$ app-> request()-> post('param1')或$ app-> request()-> params('param1')访问。

Thanks to Brian Nesbitt 感谢Brian Nesbitt

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

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