Welcome Guest, Not a member yet? Register   Sign In
Calling controller methods from inside view
#1

[eluser]louis w[/eluser]
Is there a way to call a method inside the controller from your view without having to get the CI object (get_instance()). Is there some kind of variable which references the currently loaded controller?

UPDATE:
Strange, I can access variables with the $this-> way, but not methods. Is this correct?
#2

[eluser]gtech[/eluser]
I think it bad practice to load a controller method in a view, what you should do is call the method within the controller method (or create a library) and then pass the data to the view.

I think its bad practice as you could end up calling the same method you loaded the view from, and don't think it really conforms with the MVC design approach. Also if you loaded the view from a different controller you would end up calling a method that isn't available.

So to answer your question, its deliberate that you can't call a controllers method using $this-> in a view.
#3

[eluser]metaltapimenye[/eluser]
well..here the (bad) example

controler:
Code:
<?php
class Something extends controller{
var $var;
  function Something(){
    parent :: controller();
    $this->load->library('session');
    $this->load->helper('url');
    /* other helper & lib */
    $this->var['title']=$this->title;
  }
  function index(){
  /* landing page stuff*/
  }
  function title(){
    $title=" Hello ";
    if($this->session->userdata['member_loged_in']){
      $title.=$this->session->userdata['member_name'];
    }else{
      $title.="Guest";
    }
    return $title;
  }
}?>

and in view page..:
Code:
<html>
<head>
  <title>
     <?php echo $this->var['title'];?>
  </title>
</head>
</html>
i've ever implemented in old version of CI, kind of v1.5.x.

and.. YES it is, a bad practice. cos' every function u call will processing every function that called by construction class. in other words, your system running unnecesary process that might not need.

so, handle it wisely..
#4

[eluser]gtech[/eluser]
I don't see anything wrong in your approach metaltapimenye,

you don't have to put the method in the constructor.

you can just as easily do:

Code:
function index(){
    $data=array();
    $data['title'] = $this->_title();
    $this->load->view('viewname',$data);
  }
  function _title(){
    return "hello";
  }

you would be achieving the same thing. because all you were doing is executing the function in the controller constructor and then making the return data available in the view.

maybe if you were doing this approach you could prefix the function with an _ so that you cant browse to the function. If you are calling the function everywhere its best to move it to a library or a helper (or create a base controller you can extend from).

you may be able to pass the function reference to the view.. $data['function'] = & $this->_title(); but I think that is just yuck!
#5

[eluser]metaltapimenye[/eluser]
gosh!..i just got enlightned! so thats '_' are for.. let me make it straight, so if i call $link=base_url().'_title'; .. where its gonna leads me?404 page?

the reason why i never use '_' feature all this time is.. becouse im too dumb to understand what is it for n_n.

i've take this approach becouse i need to pass a static variable name, with static key name, to every function that leads to the same view. in the other hands, im too lazy to call it in every function that browsed. (etc:dynamic menu)

Code:
function index(){
    $data=array();
    $data['title'] = $this->_title();
    $this->load->view('viewname',$data);
  }
  function _title(){
    return "hello";
  }
  function segment4(){
    #i have to call it once more
    $data['title'] = $this->_title();
    /*..other process */
    $this->load->view('viewname',$data);
  }
  function another_segment4(){
    #another calling >_<.. the case that i want to avoid
    $data['title'] = $this->_title();
    /*..other diffrent process */
    $this->load->view('viewname',$data);
  }
i havent make any libs before..let say,eww..im a bit lack of examples :red:
i still dont know for sure.. is it the more we load libs and helper is heavier our system will be?

anyhow, thx mate..n_n
#6

[eluser]gtech[/eluser]
Don't put yourself down, I am learning all the time from these forums, thats why I like the community so much.

In your case you can make a call to title in the constructor so you don't have to set it in every function. All I was saying is that you don't have to put it in the constructor.

there are loads of examples of helpers and libraries in this forum, worth trying to have a go at building a simple helper/library class as they do come in useful. if you want an example just send us a message and I will post you an example.
#7

[eluser]wiredesignz[/eluser]
Modular Extensions 4.1.16 allows callbacks from views to the currently loaded controller and its methods.

Code:
controller::instance()->method($data);
#8

[eluser]gtech[/eluser]
must admit I have not looked at Modular Extensions, would you say thats good design practice wiredesignz?
#9

[eluser]wiredesignz[/eluser]
There is no direct link so the view doesn't depend on any one controller, just a current controller with that method.

Just as a view relies on the current controller to pass it the correct $data.
#10

[eluser]gtech[/eluser]
Oh I see, so its similar to calling a library/helper function from the view?

Do you only need the method in one controller, and if so would be it available in all views called from any controller?




Theme © iAndrew 2016 - Forum software by © MyBB