TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
[SOLVED] KO3 - Calling a sub-request from a CLI request
  • Hi,

    I'm working on a RSS aggregator. I've got a controller called cli.php with a function called crawl_feeds(). This function needs a parameter to select only one type of feed. I call the function by this command line :
    php /var/www/index.php --uri=cli/crawl_feeds/type_of_feed

    In crawl_feeds(), I call a SELECT MySQL command to get the ids of the feeds I will have to crawl. Then I have to call the crawl_feed() function to crawl each feed.

    I've tried to go the HMVC way, as I've understood it… I've tried
    Request::factory('cli/crawl_feed/'.$feed_id)->execute();
    but it doesn't call for example cli/crawl_feed/2341, but /cli/crawl_feed/type_of_feed (type_of_feed being the parameter of the original request) !

    Can someone explain how to use specific parameters in a sub-request ? I know I can use $this->action_crawl_feed($id), but I want to know if there is a bug in sub-requests or if it's possible to do so.

    Thanks for your help !
  • Did you dump your $this->request object in the sub request?
    print_r($this->request)

    I've heavly mod'ed the Request object, so I can do stuff like, set multiple params in the request object e.g.
    php index.php --uri=/controller/method/ --param1=foo --param2=bar OR
    Request::factory('/controller/method')->set_param('param1','foo')->set_param('param2','bar')->execute();

    If you can't figure it out you might want to look into something like that, as far as i know the request object is stable.
  • Here are my tests…
    In the welcome controller, I add the following functions :

    public function action_request1($id) {
    Kohana::$log->add(KOHANA::DEBUG, "BEGINS 1");
    Kohana::$log->add(KOHANA::DEBUG, "ID : ".$id);
    Kohana::$log->add(KOHANA::DEBUG, Kohana::debug($this->request));
    Request::factory('welcome/request2/TWO')->execute();
    Kohana::$log->add(KOHANA::DEBUG, "ENDS 1");
    }

    public function action_request2($id) {
    Kohana::$log->add(KOHANA::DEBUG, "BEGIN 2");
    Kohana::$log->add(KOHANA::DEBUG, "ID : ".$id);
    Kohana::$log->add(KOHANA::DEBUG, Kohana::debug($this->request));
    Kohana::$log->add(KOHANA::DEBUG, "ENDS 2");
    }

    When I call
    php /var/www/index.php --uri=welcome/request1/ONE
    I get these lines in the log (I've reported only the _params of the second request) :
    …<small>protected</small> _params => <small>array</small><span>(1)</span> <span>(
    "id" => <small>string</small><span>(3)</span> "TWO"
    )</span>…


    But if I change $this->request with Request::instance() (which I thought was the same) :
    public function action_request1($id) {
    Kohana::$log->add(KOHANA::DEBUG, "BEGINS 1");
    Kohana::$log->add(KOHANA::DEBUG, "ID : ".$id);
    Kohana::$log->add(KOHANA::DEBUG, Kohana::debug(Request::instance()));
    Request::factory('welcome/request2/TWO')->execute();
    Kohana::$log->add(KOHANA::DEBUG, "ENDS 1");
    }

    public function action_request2($id) {
    Kohana::$log->add(KOHANA::DEBUG, "BEGIN 2");
    Kohana::$log->add(KOHANA::DEBUG, "ID : ".$id);
    Kohana::$log->add(KOHANA::DEBUG, Kohana::debug(Request::instance()));
    Kohana::$log->add(KOHANA::DEBUG, "ENDS 2");
    }

    I get the following logs :
    …<small>protected</small> _params => <small>array</small><span>(1)</span> <span>(
    "id" => <small>string</small><span>(3)</span> "ONE"
    )</span>…


    The id is still "ONE" but should be "TWO"…
  • They aren't same, Request::factory() creates new request instance, while Request::instance() only returns the top request in the hierarchy.
  • OK this is clearer ;-)