1

I am trying to use Kohana 3.3 HMVC approach. I have created a view and a controller for generating a segment of a page (meant to be integrated into the actual web page by another controller and to be never accessed through HTTP from outside a controller) filled with data records retrieved through ORM. What I need is to pass some data (records selection criteria) from the top controller through the middle controller to the ORM model. Altering GET/POST data as suggested here seems wacky (as it is going to alter the whole application state rather than of the target controller as far as I understand) (but perhaps it is considered okay in the PHP world, I don't know (I am coming from the strict C++/C#/Java/Scala world), let me know if so). Is there a better way?

Ivan
  • 63,011
  • 101
  • 250
  • 382

1 Answers1

0

The HMVC approach works just like a normal request except that it has its own instance of the request class. From the HMVC sub request you can access the parent request object by loading the initial request.

 $parent_request_query = $this->request->initial()->query();

You can also access the current request.

 $current_request_query = $this->request->current()->query();

You could also just pass parameters.

 $params = array(
      'passing' => 'data' 
 );
 echo Request::factory('controller/action')->query($params)->execute()->body()
Peter
  • 2,276
  • 4
  • 32
  • 40
  • So you say that the way I am meant to pass data to a controller B when I call from controller A is setting fields of the controller A (like public properties of it or actually modifying GET/POST) and reading them in the controller B? – Ivan Feb 03 '14 at 22:17
  • 1
    That is how I do it. I try and keep each sub HMVC segment as independent as possible. Having the child segment grab its variables on its own keeps it very independent. – Peter Feb 04 '14 at 01:53
  • By the way, can you explain why `$this->request->current()->query('id');` in a sub-controller (one called through `Request::factory`) but `$this->request->param('id');` in the top controller (one invoked directly through http request routing)? The second version doesn't work in the first case and the first one doesn't work in the second (I have tried both in both cases seeking to use one same for both to make the code prettier). – Ivan Feb 10 '14 at 05:27
  • When calling params that are passed to a sub request, I use $this->request->query(). If I wanted to get to the original query params, I would use $this->request->initial()->query(). But, Im not entirely sure if I understand what you are asking. Perhaps you can post a more detailed question. – Peter Feb 13 '14 at 01:44