php 如何使用Kohana的$this->request->param来获取请求变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5621747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use $this->request->param of Kohana to get request variables
提问by Vivek Goel
I have written a sample controller in kohana
我在 kohana 中编写了一个示例控制器
<?php
defined('SYSPATH') OR die('No direct access allowed.');
class Controller_Album extends Controller {
public function action_index() {
$content=$this->request->param('id','value is null');
$this->response->body($content);
}
}
But when I am trying to hit url http://localhost/k/album?id=4I am getting NULL value. how can I access request variable in kohana using request->param and without using $_GET and $_POST method ?
但是当我尝试访问 url http://localhost/k/album?id=4 时,我得到了 NULL 值。如何使用 request->param 而不使用 $_GET 和 $_POST 方法访问 kohana 中的请求变量?
回答by biakaveron
In Kohana v3.1+ Request class has query()
and post()
methods. They work both as getter and setter:
在 Kohana v3.1+ 请求类中有query()
和post()
方法。它们既可以作为 getter 也可以作为 setter:
// get $_POST data
$data = $this->request->post();
// returns $_GET['foo'] or NULL if not exists
$foo = $this->request->query('foo');
// set $_POST['foo'] value for the executing request
$request->post('foo', 'bar');
// or put array of vars. All existing data will be deleted!
$request->query(array('foo' => 'bar'));
But remember that setting GET/POST data will not overload current $_GET/$_POST values. They will be sent after request executing ($request->execute()
call).
但请记住,设置 GET/POST 数据不会使当前的 $_GET/$_POST 值过载。它们将在请求执行($request->execute()
调用)后发送。
回答by SpadXIII
In Konana (3.0) you can't access $_GET/$_POST through the Request class. You'll have to directly use $_GET/$_POST
在 Konana (3.0) 中,您无法通过 Request 类访问 $_GET/$_POST。你必须直接使用 $_GET/$_POST
$this->request->param('paramname', 'defaultvalue')
is for accessing params defined in the route. For route-urls like <controller>/<action>/<id>
you would use $this->request->param('id')
to access the portion in the route url.
$this->request->param('paramname', 'defaultvalue')
用于访问路由中定义的参数。对于路由 url,就像<controller>/<action>/<id>
您$this->request->param('id')
用来访问路由 url 中的部分一样。
edit: in Kohana 3.1 there are post
and query
methods for getting/setting data of the request; check the documentation at http://kohanaframework.org/3.1/guide/api/Request
编辑:在Kohana的3.1有post
和query
来获得/设置请求的数据的方法; 检查http://kohanaframework.org/3.1/guide/api/Request 上的文档
回答by Kemo
Notice that altough it's more clear to use $this->request->param(), you can define action params as :
请注意,虽然使用 $this->request->param() 更清楚,但您可以将操作参数定义为:
public function action_index($id, $seo = NULL, $something = NULL)..
and access those vars directly. You have to define these vars in the same order they're defined in the corresponding route (excluding the action and controller params, they're defined on request level anyways so there's no need to pass them to the action method).
并直接访问这些变量。您必须按照在相应路由中定义的相同顺序定义这些变量(不包括操作和控制器参数,它们无论如何都是在请求级别定义的,因此无需将它们传递给操作方法)。
EDIT: This functionality was deprecated in 3.1 and has been removed from 3.2, so it is best to avoid. You can read more here: http://kohanaframework.org/3.2/guide/kohana/upgrading#controller-action-parameters
编辑:此功能在 3.1 中已弃用,并已从 3.2 中删除,因此最好避免。您可以在此处阅读更多信息:http: //kohanaframework.org/3.2/guide/kohana/upgrading#controller-action-parameters
回答by pconcepcion
If I remember well, if you have not changed the default routes, you can try using the url http://localhost/k/album/4with that controller.
如果我没记错的话,如果您没有更改默认路由,您可以尝试在该控制器上使用 url http://localhost/k/album/4。
As the default route is in the form: /<controller>/<action>/<id>
由于默认路由采用以下形式: /<controller>/<action>/<id>
Hope it helps.
希望能帮助到你。