Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call codeigniter controller function from view

How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.

like image 771
keerthi Avatar asked Apr 03 '12 02:04

keerthi


People also ask

How do I load helper into view?

You can call the helper function in Codeigniter using the function name in the view file. To call a helper method you have to first create a helper method in the helper file, autoload it in the base controller or controller's method after that you can access it in your controller and view files.


2 Answers

You can call controller function from view in the following way:

Controller:

public function read() {     $object['controller'] = $this;      $this->load->view('read', $object); } 

View:

// to call controller function from view, do $controller->myOtherFunct(); 
like image 115
saad Avatar answered Sep 30 '22 00:09

saad


Codeigniter is an MVC (Model - View - Controller) framework. It's really not a good idea to call a function from the view. The view should be used just for presentation, and all your logic should be happening before you get to the view in the controllers and models.

A good start for clarifying the best practice is to follow this tutorial:

https://codeigniter.com/user_guide/tutorial/index.html

It's simple, but it really lays out an excellent how-to.

I hope this helps!

like image 45
envysea Avatar answered Sep 30 '22 00:09

envysea