View model binder for bind request data to view model class in codeigniter
- You need only libraries folder.
- Copy Request.php file to codeigniter application/libraries .
- Add file name into autoloader in codeigniter application/config/autoload.php file:
$autoload['libraries'] = array(
'Request'
);Create a class for bind GET/POST request data For example create Employee view model
class EmployeeViewModel
{
public $firstName;
public $lastName;
public $age;
public $salary;
}Next in your html file add some input :
<form action="<?php echo base_url("DemoController/add_submit") ?>" method="POST">
<input type="text" name="firstName" />
<input type="text" name="lastName" />
<input type="text" name="age" />
<input type="text" name="salary" />
<input type="submit" />
</form>Next in your Controller/Action bind your request data with view model(Employee) :
class DemoContoller extends CI_Controller
{
public function add_submit()
{
$viewModel = new EmployeeViewModel();
Request::bind($viewModel);
// now $viewModel filled with request data
echo $viewModel->firstName . $viewModel->lastName;
}
} Request::bind($viewModel,Request::POST); // POST request method , it's default
Request::bind($viewModel,Request::GET); // GET request method// GET request method without xss filtering
Request::bind(
$viewModel,
Request::GET,
false
); // GET request method without xss filtering and skip salary
Request::bind(
$viewModel,
Request::GET,
false,
array('salary')
);