CodeIgniter Forums
invoke controll from view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: invoke controll from view? (/showthread.php?tid=39295)

Pages: 1 2


invoke controll from view? - El Forum - 03-07-2011

[eluser]Smy[/eluser]
Can we invoke controller from a view?


invoke controll from view? - El Forum - 03-07-2011

[eluser]oldmatt[/eluser]
I'm not sure I understand why you would want to.

Can you explain what you are trying to accomplish? There might be an easier way to do it.


invoke controll from view? - El Forum - 03-07-2011

[eluser]oldmatt[/eluser]
In general, the data should already be "controlled" by the time the view needs it.


invoke controll from view? - El Forum - 03-07-2011

[eluser]dark_lord[/eluser]
Yes, I believe you can invoice controller in a view and actually there are alot of ways! one is..

Code:
<a href="&lt;?php echo site_url('controller/method'); ?&gt;">Invoke Me</a>

...and

&lt;?php

echo anchor('controller/method', 'title');

?&gt;



invoke controll from view? - El Forum - 03-07-2011

[eluser]oldmatt[/eluser]
Perhaps I misunderstood...

I understood the term "invoke" means to call a method on the controller itself. The code mentioned calls helper functions and not not controller methods.


invoke controll from view? - El Forum - 03-07-2011

[eluser]diego6616[/eluser]
You could pass a reference of your controller to your view, then invoke the method. Be aware that it is not a good practice in MVC styled-code, but It will work for a fast-fix, until you move the code to a helper or something like that (I used it for that once).

You shoud do in your controller method:

Code:
$view_vars['controller'] = &$this;
$this->load->view('your_view_file', $view_vars);

Then in your view you can do:

Code:
$controller->method($parameter1, $parameter2, 'parameter3', [...]);



invoke controll from view? - El Forum - 03-07-2011

[eluser]Smy[/eluser]
Thank you all for giving me a quick response. actually i am new to codeigniter. My requirement is very basic one.i am developing a test application for displaying latest news from database.
here is the controller 'news.php'.
Code:
function index()
    {
        $this->load->model('NewsModel','newsObj');
        $result['news'] = $this->newsObj->getNews(); // function to retrieve the news
        $this->load->view('news', $result);
    }
and this is my view 'news.php'

Code:
echo meta('Content-type', 'text/html; charset=utf-8', 'equiv');
foreach ($news as $row)
{
    echo $row->chvNewsTitle;
    echo br(1);
  
}

i want to check the news expiration date with current date before printing the news (before the line 'echo $row->chvNewsTitle;'). i don't want to compare it in the query. i created a function for date comparison in the controller 'news.php'. i don't know how to call the function in the controller from the view.

Hope you understand my situation...


invoke controll from view? - El Forum - 03-08-2011

[eluser]diego6616[/eluser]
in news.php (controller):

Code:
function index()
    {
        $this->load->model('NewsModel','newsObj');
        $result['news'] = $this->newsObj->getNews(); // function to retrieve the news
        $result['controller'] = &$this; // pass a reference to $this, that in this context represents the current controller's instance (and the main CI "god" object as well).
        $this->load->view('news', $result);
    }

in view ‘news.php’

Code:
echo meta('Content-type', 'text/html; charset=utf-8', 'equiv');
foreach ($news as $row)
{
    if($controller->your_method($row->date)) {
      echo $row->chvNewsTitle;
      echo br(1);
    }  
}

I assume your date function will return true if the date is on the expected range, false otherwise.
Hope it helps.


invoke controll from view? - El Forum - 03-08-2011

[eluser]Smy[/eluser]
Thank u so much diego6616. Now it is working..But u told it is not a good practice in MVC then is there any other way to do this?


invoke controll from view? - El Forum - 03-08-2011

[eluser]diego6616[/eluser]
Your Views should never call a method in the controller.

The basic approach is:
Models: retrieve the data, from a database, web service, file, etc.
Controllers: attend the HTTP request. Do the "application stuff" logic (basically: control all the other classes and objects).
Views: present the data in HTML.

So the filtering should be done in your model at best, controller: ok, view: never. That way, if you retrieve data, let's say, from the same table but with other filters, you could use the same view again, instead of writing one from scrap, just to make room for the filter.
Also, generally it's better to retrieve the minimum data possible from the database. That way you don't load all on PHP runtime memory just to filter it. The database can do it for you, and probably much better than PHP because it's made to handle large sets of data.
Also if you want to have a function that it's not directly connected to a controller (you probably will need again that date comparison function on other controller or view as well), it's better to locate it on a helper. Read the helper chapter on the docs, those are very easy to use and are classic procedural programming (with a few tweaks).
I know it sound like "that's a lot of code to write", but believe me, on a medium-to-large proyect, it is a bless, because generally it's easy to determine where is the code you should modify if there is a change on the app.

Anyway, if you want to do it your way, CI will let you.

Best regards.
Diego.