CodeIgniter Forums
Run another controller's method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Run another controller's method (/showthread.php?tid=6042)

Pages: 1 2 3


Run another controller's method - El Forum - 02-13-2008

[eluser]Sam Dark[/eluser]
Hello, everyone!

Is it possible to reroute application flow to another controller/action?

Code:
class A extends Controller {
  function index($id, $name){
    $param['id'] = $id;
    $param['name'] = $name;
    $this->delegate('b', 'say_it', $param);
  }
}

class B extends Controller {
  function say_it($id, $name){
    print 'Hello, '.$name.'! Your id is '.$id.'.';
  }
}



Run another controller's method - El Forum - 02-13-2008

[eluser]wiredesignz[/eluser]
Nothing is impossible, but you could research redirect() in the user guide.


Run another controller's method - El Forum - 02-13-2008

[eluser]Sam Dark[/eluser]
redirect() from URL Helper? It just redirects with http headers or meta.

I need to reroute application flow, not to redirect!

Code:
class A extends Controller {
  function index($id, $name){
    $param['id'] = $id;
    $param['name'] = $name;
    $this->delegate('b', 'say_it', $param);
  }
}

class B extends Controller {
  function say_it($id, $name){
    print 'Hello, '.$name.'! Your id is '.$id.'.';
    $this->delegate('c', 'doit');
  }
}

class C extends Controller {
  function doit(){
    print 'done';
  }
}



Run another controller's method - El Forum - 02-13-2008

[eluser]xwero[/eluser]
Can you give a real world example where this sort of thing would be useful. Maybe there are other ways to solve this.


Run another controller's method - El Forum - 02-13-2008

[eluser]Chris Newton[/eluser]
You could create a custom library, which is effectively a controller, then load it in any controller you need. That's a better practice than pulling stuff out of some other random controller.


Run another controller's method - El Forum - 02-13-2008

[eluser]jbowman[/eluser]
I imagine he's talking about a portal implementation, where you might want to use multiple controllers to create one pageview. ie: forumsController for the most recent forums posts, newsController for the most recent news items, and blogsController for the most recent blogs.

This is something I'll be looking into at some point, so if someone has a good solution, I'd be intereted also.


Run another controller's method - El Forum - 02-13-2008

[eluser]Chris Newton[/eluser]
what's wrong with the library idea? It's exactly what you're talking about.


Run another controller's method - El Forum - 02-13-2008

[eluser]wiredesignz[/eluser]
mahuti is absolutely right, if you need common functionality between controllers, use a library.


Run another controller's method - El Forum - 02-15-2008

[eluser]Sam Dark[/eluser]
Sorry for not replying for a while.

jbowman is right: I'm trying to implement potal-like View library so it will be able to handle something like this:

Code:
<html>
  ...
  <body>
    <div class="sidemenu">
      <h2>Recent posts</h2>
      &lt;?=call_controller('blog', 'recent', array('count' => 10))?&gt;
      <h2>Recent comments</h2>
      &lt;?=call_controller('comments', 'recent', array('count' => 10))?&gt;
    </div>
    <div class="content">
      &lt;?=$content?&gt;
    </div>
  &lt;/body&gt;
&lt;/html&gt;

Calling blog controller's recent action with an array of options and returning output.

Why I don't want to implement those recent posts/blog as library?

1. I don't want to write and load library for each of these blocks.
2. I want to use controller's output on it's own page also.


Run another controller's method - El Forum - 02-15-2008

[eluser]Sam Dark[/eluser]
Just found related topic here: http://ellislab.com/forums/viewthread/70425/P45/