Is there any way to pass value from one controller to model as GET or POST
I have 1 controller V1 and 1 model Vmodel
class V1 extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Vmodel','',TRUE);
}
/* index action */
public function index()
{
$user='abcd';
$details = $this->Vmodel->login();
}
}
model
class V1Modelnew extends CI_Model{
public function __construct() {
}
public function login()
{
//here, I need to get the user variable as post
echo $_POST['user'];
}
}
I need to get the value 'abcd' as POST in model. Any method to do this?
Errors in your code
$this->load->model('Vmodel'
But you define it has V1Modelnew
$user
value to your model function $_POST['user']
In controller
class V1 extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('V1Modelnew'); # Changed
}
public function index()
{
$user='abcd';
$details = $this->V1Modelnew->login($user); # Changed
}
}
In Model
class V1Modelnew extends CI_Model{
public function __construct()
{
}
public function login($user) # Changed
{
echo $user; # Changed
}
}
There is an architectural problem here. You don't want your model accessing values in the $_POST or $_GET. This couples your model layer to the HTTP layer of your application.
Instead, you want to pass the necessary values into your model as function arguments.
Your controller:
$details = $this->Vmodel->login('abcd', $_POST['password']);
And your model:
public function login($username, $password) {
// return $details
}
Try in this way
class V1 extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Vmodel','',TRUE);
}
/* index action */
public function index()
{
$user=$this->input->post('user');
$details = $this->Vmodel->login($user);
}
}
Model:
class Vmodel extends CI_Model{ //use Vmodel NOT V1Modelnew
public function __construct() {
}
public function login($user)
{
echo $user;
}
}
If you want to user post
and get
you need to do it in Controller in this way:
$value = $this->input->post('value_name'); // for Post
$value = $this->input->get('value_name'); // for GET
For more, read documentation
Controller
class V1 extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Vmodel','',TRUE);
}
/* index action */
public function index()
{
$user='abcd';
$details = $this->Vmodel->login($_POST);
}
}
Model
class V1Modelnew extends CI_Model{
public function __construct() {
}
public function login($data='')
{
//here, I need to get the user variable as post
echo $_POST['user'];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.