Run another controller's method

#21
[eluser]Phil Sturgeon[/eluser]
[quote author="Sam Dark" date="1203118486"]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.[/quote]

What you are describing here is the use of a model. This looks like you are just pulling and returning some data basedo na few parameters. That is what a model does. A controller is simply for working out which models, libraries and views to load based on a URL.

But if you must be awkward and do it your way, simply use a include() and call the class.

Example:

Code:
&lt;?php
class Blogs extends Controller {

function some_page() {
  include('./other_controller.php')

  $other_controller = Other_controller();
  $values = $other_controller->some_method();
}

}
?&gt;

I almost feel dirty for showing you that. There is NO case in which you need to do this. If you think you do, you are doing it wrong. Guaranteed.

#22
[eluser]Phil Sturgeon[/eluser]
Ahaha! Just spotted the date. Whoops...

#23
[eluser]Dam1an[/eluser]
Yeah, this post was resurected by a spammer :grrr:
And I don't subscribe to any threads either, but still get them under new posts

#24
[eluser]louis w[/eluser]
Check out the Wick Library

Have not had a chance to use it, but looks cool.

#25
[eluser]Unknown[/eluser]
try hmvc! ive ever faced the same problem,and hmvc gave me great solution.you could build modular portal with it

#26
[eluser]metaltapimenye[/eluser]
model. thats to simplify the answer.

"put a method in a controller" that was a problem here. just put your method in model part. thats why CI running MVC at the first stone.

controller
Code:
function thread($par1,$par2){
$this->load->view('template/thread');
}

model:
Code:
function recent_post($top_limit){
  #..fetching recent post
}
function recent_comment($top_limit){
  #..fetching recent coment
}
function related_thread($string_to_relate,$limit){
  #..fetching related thread
}
view:
Code:
&lt;?php $this->load->model('fetcher_model','model');?&gt;
&lt;body&gt;
    <div class="sidemenu">
      <h2>Recent posts</h2>
      &lt;?php echo $this->model->recent_post('10');?&gt;
      <h2>Recent comments</h2>
      &lt;?php echo $this->model->recent_comment('10');?&gt;
       <h2>Recent comments</h2>
      &lt;?php echo $this->model->related_thread($row['current_title'],5);?&gt;
    </div>
    <div class="content">
      &lt;?=$content?&gt;
    </div>
  &lt;/body&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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