Felipe Moreira
Felipe Moreira

Reputation: 81

How can I get the variable in the model for the controller?

I have a $send variable in my model. I want to use it on the controller. But I get an error. How can I call it to the controller?

Model:

public function register_user($send)
  {

    if($this->emailVerify()) {
      $send = array(
        'tipo' => 'Error.'
      );
      return true;

    } else {
      return false;
    }

Controller:

   public function __construct()
    {
        parent::__construct();

        $this->send;
    }
    public function register()
        {
            $this->load->model('users');
            $this->users->register_user();
            $this->load->view('signup', $this->send);
        }

Upvotes: 1

Views: 46

Answers (3)

nice_dev
nice_dev

Reputation: 17805

You can declare a private variable, say send in your model and make getter and setter in your model class in an encapsulated way to get the value in the controller, like below:

Snippet:

Model:

<?php

class Yourmodel extends CI_Model{
    private $send;
    function __construct() {
        parent::__construct();
        $this->send = [];
    }

    public function register_user($send){
        if($this->emailVerify()) {
          $this->send = array(
            'tipo' => 'Error.'
          );

          return true;
        } 

        return false;
    }

    public function setSendValue($value){
        $this->send = $value;
    }

    public function getSendValue(){
        return $this->send;
    }
}

Controller:

<?php

class Controller extends CI_Controller{
    private $send;
    public function __construct(){
        parent::__construct();
        $this->send = [];
    }

    public function register(){
        $this->load->model('users');
        if($this->users->register_user()){
            $this->send = $this->users->getSendValue();
        }
        $this->load->view('signup', $this->send);
    }
}

Upvotes: 2

Model

public function register_user($send = "")
  {

    if($this->emailVerify()) {
      $send = array(
        'tipo' => 'Error.'
      );
      return $send;

    } else {
      return false;
    }

Controller

public function __construct()
    {
        parent::__construct();

        $this->send;
    }
    public function register()
        {
            $this->load->model('users');
            $sendRes = $this->users->register_user(); //now you can use this $send response variable
            $this->load->view('signup', $this->send);
        }

Upvotes: 0

Farhan
Farhan

Reputation: 253

Replace your code modal and controller as follows:

  1. You need not to declare $send in modal definition, as you are not passing any value while calling the same modal function.
  2. modal positive return can be array $send itself
  3. Catch value of modal function

Modal :

public function register_user()
  {

    if($this->emailVerify()) {
      $send = array(
        'tipo' => 'Error.'
      );
      return $send;

    } else {
      return false;
    }

Controller:

       public function __construct()
        {
            parent::__construct();

            $this->send;
        }
        public function register()
            {
                $this->load->model('users');
                $send = $this->users->register_user();
        //print_r($send); // You will get data here
                $this->load->view('signup', $this->send);
            }

Upvotes: 0

Related Questions