简体   繁体   中英

MVC Codeigniter - How is the controller called from the view?

I'm still new to mvc, working on a project using the codeigniter framework. I have searched through a lot of tutorials and stackoverflow posts and I still cannot find a simple answer to a very simple question. How is the the controller called from the views. Is it just an ajax call from every view page and POST the required controller url?

I understand the concept of mvc but just cannot figure out the best way to call a controller based on selecting something on the view. Maybe it is just a simple ajax call and I'm overthinking it but a some clarification on the matter would be greatly appreciated.

Example:

I have a form view loaded. When the user select the 'next' button on the form I want the controller to be called that loads the next view after the form. Do you just trigger a POST to the controller url when the button is clicked? That is my question. Maybe that is the way to do it but it hasn't been clarified anywhere i've read.

What you're missing is actually the routing structure, this dictates how the controllers load up the views depending on a specific URL request.

Say you have this url: example.com/products

This will look for a controller with a class of Products . Also, it will default look for a function called index within the controller by default. We will also want to pull down all of the products from our products table, so we'll need to have our associated model. let's have a look at what that looks like:

class Products extends CI_Controller {
    public function index(){
        //load our Products model
        $this->load->model('Model_products');

        //let's get all of our products off of our model
        $data['products'] = $this->Products->all();

        //now let's return all of our products with our view `products`
        $this->load->view('products', $data);
    }
}

Of course we need our associated model in application/models/products_model.php , with an all function that retrieves all products from the database.

class Products_model extends CI_Model {
    public function __construct(){
        parent::__construct():
    }

    public function all(){
        return $this->db->get('products');
    }
}

Hopefully this helps give you some clarity on how the MVC pattern works with internal routing .

as mentioned earlier you don't call a controller from the view normally. That goes against MVC. What you can do, if I am getting your question right, you can add a URL that points to your controller as a value to your select option tag. Than you can handle the selection from javascript as an onselect event. You just give the selected value to window.location.href and you will be redirected to your controller.

According to the mvc standards, views shouldn't access controllers methods directly. If you want to bring another class method result to your view, you should call it inside your controller and store it in a variable, so you can access it inside the view.

Example using code igniter:

//controller:
$data = array(
    'name' => 'Carlos',
);
$this->load->view('myview', $data);

and then:

//view
echo $name; //prints 'Carlos'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM