CodeIgniter POST/GET 默认值
CodeIgniter POST/GET default value
我可以为 POST/GET 数据设置默认值吗,如果它是 empty/false,比如
$this->input->post("varname", "value-if-falsy")
?
所以我不必像
那样编写代码
$a = $this->input->post("varname") ?
$this->input->post("varname") :
"value-if-falsy"
您必须覆盖默认行为。
在application/core
中创建MY_Input.php
class MY_Input extends CI_Input
{
function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
$ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
if(!$ret_val)
$ret_val = $default_value;
return $ret_val;
}
}
然后在你的控制器中:
$this->input->post("varname", "", "value-if-falsy")
它对我有用,我使用了@AdrienXL 技巧。
只需创建 application/core/MY_Input.php
文件并调用父方法(您可以在 CodeIgniter 系统文件夹 system/core/Input.php
:
中找到此方法
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Input extends CI_Input
{
function post($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::post($index, $xss_clean);
return ($value) ? $value : $default_value;
}
function get($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::get($index, $xss_clean);
return ($value) ? $value : $default_value;
}
}
所以,调用方法时,传入默认值:
$variable = $this->input->post("varname", "value-if-falsy");
您可以将代码块简化如下
public function post($index = NULL, $xss_clean = NULL, $default_value = NULL )
{
if( is_null( $value = $this->_fetch_from_array($_POST, $index, $xss_clean ) ) ){
return $default_value;
}
return $value;
}
不久前才发现我也可以用?:
,例如
$name = $this->input->post('name') ?: 'defaultvalue';
您可以在 set_rules
之前使用此代码
empty($_POST['varname']) ? $_POST['varname'] = 'value if empty' : 0;
我可以为 POST/GET 数据设置默认值吗,如果它是 empty/false,比如
$this->input->post("varname", "value-if-falsy")
?
所以我不必像
那样编写代码$a = $this->input->post("varname") ?
$this->input->post("varname") :
"value-if-falsy"
您必须覆盖默认行为。
在application/core
中创建MY_Input.php
class MY_Input extends CI_Input
{
function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
$ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
if(!$ret_val)
$ret_val = $default_value;
return $ret_val;
}
}
然后在你的控制器中:
$this->input->post("varname", "", "value-if-falsy")
它对我有用,我使用了@AdrienXL 技巧。
只需创建 application/core/MY_Input.php
文件并调用父方法(您可以在 CodeIgniter 系统文件夹 system/core/Input.php
:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Input extends CI_Input
{
function post($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::post($index, $xss_clean);
return ($value) ? $value : $default_value;
}
function get($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::get($index, $xss_clean);
return ($value) ? $value : $default_value;
}
}
所以,调用方法时,传入默认值:
$variable = $this->input->post("varname", "value-if-falsy");
您可以将代码块简化如下
public function post($index = NULL, $xss_clean = NULL, $default_value = NULL )
{
if( is_null( $value = $this->_fetch_from_array($_POST, $index, $xss_clean ) ) ){
return $default_value;
}
return $value;
}
不久前才发现我也可以用?:
,例如
$name = $this->input->post('name') ?: 'defaultvalue';
您可以在 set_rules
之前使用此代码empty($_POST['varname']) ? $_POST['varname'] = 'value if empty' : 0;