Slim Framework 2.0.0无法使用 - > params()和GET [英] Slim Framework 2.0.0 Unable to use ->params() with GET

查看:123
本文介绍了Slim Framework 2.0.0无法使用 - > params()和GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SLIM 2.0.0



是否可以使用 - > params()和GET?



在下面的例子中:


  • 如果我通过POST调用它: curl -dparam1 = hello& amp ; param2 = worldhttp:// localhost / foo 它打印:helloworld CORRECT !!

  • 如果我通过GET调用它: http:// localhost / foo / hello / world 它打印:没有任何东西! < - 错误!



为什么?

 <?php 
要求'Slim / Slim.php';

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

函数foo(){
$ request = \Slim\Slim :: getInstance() - >请求();
echo $ request - > PARAMS(参数1’ );
echo $ request - > PARAMS(参数2’ );
}
?>


解决方案

求解!
在文档页面请求变量 - Slim框架文档我读到:


一个HTTP请求可能有相关的变量(不要与路由变量混淆)。使用当前HTTP请求发送的GET,POST或PUT变量通过Slim应用程序的请求对象公开。



如果您想快速获取请求变量值而不考虑它的类型,使用请求对象的params()方法:

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

params()方法将首先搜索PUT变量,然后搜索POST变量,然后搜索GET变量。如果找不到变量,则返回null。如果您只想搜索特定类型的变量,则可以使用这些方法:

 <?php 
//获取请求对象
$ req = $ app-> request();

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

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

所以:

关键是一个HTTP请求可能有相关的变量(不要与路由变量混淆)。

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

在上述URI中读取路由变量/参数来自'/ foo / hello / world'部分。请求GET变量是从查询字符串('name = brian')读取的,可以通过$ app-> request() - > get('name')或$ app-> request() - > params('名称')。
$ b

请求POST变量从请求正文解析并且可以被访问$ app-> request() - > post('param1')。或$ app-> request() - > params('param1')。

感谢Brian Nesbitt


I'm using SLIM 2.0.0

Is it possible to use ->params() with GET?

In the example below

  • if I call it by POST: curl -d "param1=hello&param2=world" http://localhost/foo it prints: helloworld CORRECT!!
  • if I call it by GET: http://localhost/foo/hello/world it prints: 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:

An HTTP request may have associated variables (not to be confused with route variables). The GET, POST, or PUT variables sent with the current HTTP request are exposed via the Slim application’s request object.

If you want to quickly fetch a request variable value without considering its type, use the request object’s params() method:

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

The params() method will first search PUT variables, then POST variables, then GET variables. If no variables are found, null is returned. 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://domain.com/foo/hello/wold?name=brian

In the above URI the route variables/parameters are read from the '/foo/hello/world' portion. 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').

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').

Thanks to Brian Nesbitt

这篇关于Slim Framework 2.0.0无法使用 - &gt; params()和GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆