Run another controller's method

#1
[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.'.';
  }
}

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

#3
[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';
  }
}

#4
[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.

#5
[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.

#6
[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.

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

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

#9
[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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.