How to Call Codeigniter Controller Function from View

How to call codeigniter controller function from view

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!

I need to call a controller function inside a view -Codeigniter

I am not sure if you use CodeIgniter 2 or 3.

Anyway, basically you don't want to use anything inside View files except perhaps helper function(s) or some kind of "presenter" layer (that should be called inside controller I guess).


Solution using Join

Go and read this manual page and search for join. There you can learn about implementation of SQL join directive.

You want to modify this (getCategories()) function so it returns data that you require

function getCategories() {

$this->db->select('category.title, category.no, user.name as username')
->from('category')
->join('user', 'user.id = category.id');
$query = $this->db->get();

if ($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}

and in view you can get your username like this

foreach ($categories as $object) {
echo $object->title . "  ". $object->no . $object->username . '<br/>';
}

I am not 100% sure so please post comments I will edit this answer later.


Solution "breaking rules"

https://stackoverflow.com/a/24320884/1564365


general notes

Also consider naming your tables using plural so categories, users...

Also it is a bad practise to use "category.id as user.id" (storing user id inside category table in "id" field) instead you shold use either a pivot table or in case of 1:1 relation field "user_id".

Calling a Codeigniter controller function on view load

You can use CodeIgniter hooks:

https://ellislab.com/codeigniter/user-guide/general/hooks.html

Untested, but this is how I think it must work. Taking this from that quora link I put in the comments, it looks like when you call $controller =& get_instance(); then that is the controller and you can add a property to it and it will be there for your view.

I'd play with the different options as well, this could be promising depending on what you are doing in your __construct method:

post_controller_constructor
Called immediately after your controller is instantiated, but prior to any method calls happening.

class Blog extends CI_Controller{    

public $balance = 0;

public function getBalance(){
$this->balance = Balance::getMe();
}
}

What you can do is call the get_instance helper function from your hook.

class PostControllerHook{  
function post_controller($params) {
// $params[0] = 'controller' (given the params in the question)
// $controller is now your controller instance,
// the same instance that just handled the request
$controller =& get_instance();
$controller->getBalance();
}
}

then use

$this->load->view('navbar', $this->balance);

Codeigniter 4 How to call a controller method in a view?

You're calling pr wrong:

  1. pr is a method in your \App\Controller\Home class. So it should be \App\Controller\Home::pr(). Instead, you were calling \CodeIgniter\View\View::pr().

  2. Views are probably rendered in to some \CodeIgniter\View\View class instance, hence calling $this in a view results in calling \CodeIgniter\View\View::pr(). That's why a $this->pr() call isn't working as you expected.

  3. Your Home::pr() is an ordinary method, which can only be called within the instance (i.e. through $this inside an instance). Your view do not have access to it.

  4. It seems your pr is not doing anything specific to the Controller instance (i.e. not using $this inside), you can simply rewrite it as a public static method, which can easily be called in your view.

So, this is probably useful for you.

Controller:

namespace App\Controllers;

...

class Home {

...

public static function pr($array, $die = "", $type = "")
{
ini_set("xdebug.var_display_max_children", '-1');
ini_set("xdebug.var_display_max_data", '-1');
ini_set("xdebug.var_display_max_depth", '-1');

echo "<pre>";
if (!$type) print_r($array);
else var_dump($array);
echo "</pre>";
if ($die) die();
}

...

}

View:

 <div class="col-md-9">
<?php \App\Controllers\Home::pr("11", 22); ?>

How to call codeigniter controller function from view

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!



Related Topics



Leave a reply



Submit