Application::in
Get request data (POST/GET)
Description
public string|array Application::in( string $name = '', string $method = '' )
Get request data from $_GET and $_POST.
- With
$name, returns the value of that parameter (a string, or an array for parameters likeids[]). If the parameter is missing or an empty string, returns''. - Without
$name, returns all parameters as an array (''when there are none). - If GET and POST have a parameter with the same name, POST wins.
The value is returned as it was sent. It is not escaped: validate it before use, pass it to the database only through ? placeholders, and let the template escape it on output.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $name | string | '' | Request parameter name. All parameters when omitted |
| $method | string | '' | 'get' or 'post' (case-insensitive) to read only one of them. Both when omitted |
Example
Controller - GET
// https://example.com/?foo=get
echo $this->in('foo'); // -> get
echo $this->in('foo', 'get'); // -> get
echo $this->in('foo', 'post'); // -> '' (empty string)
Controller - POST
// https://example.com/ : POST('foo' => 'post')
echo $this->in('foo'); // -> post
echo $this->in('foo', 'get'); // -> ''
echo $this->in('foo', 'post'); // -> post
If POST and GET have a parameter with the same name, POST wins.
Controller - GET and POST
// https://example.com/?foo=get : POST('foo' => 'post')
echo $this->in('foo'); // -> post
echo $this->in('foo', 'get'); // -> get
echo $this->in('foo', 'post'); // -> post
You can get all request data by omitting the first argument.
Controller
// https://example.com/ : POST('foo' => 'bar', 'baz' => 'qux')
var_dump( $this->in() );
Output
array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(3) "qux" }
A typical use is the page number of a list:
htdocs/_fegg/site/controller/News.php
$this->page['data'] = $this->_model
->listMax(20)
->current($this->in('p'))
->order('post_date', 'desc')
->find();