Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I preserve the value of a variable after an HMVC sub-request in Kohana 3.1?
    primarykey
    data
    text
    <p>I'm having an issue with preserving the value of a variable after an HMVC sub-request in Kohana 3.1.3.1 and am wondering how best to approach/fix it. I thought that additional requests in Kohana were isolated from each other, but it doesn't seem to be the case...</p> <p>First of all, I've created a controller to extend the Controller_Template:</p> <pre><code>abstract class Controller_Website extends Controller_Template { public $page_info; public $allow_caching; public function before() { // ... more unrelated code here ... // Only do this if auto_render is TRUE (default) if ($this-&gt;auto_render === TRUE AND $this-&gt;request-&gt;is_initial()) { // Set our Page_Info to the values we just loaded from the database $this-&gt;page_info = clone $this-&gt;navs[$slug]; } // ... more unrelated code here ... } public function after() { // ... more unrelated code here ... // For internal requests, let's just get everything except for the template if (! $this-&gt;request-&gt;is_initial()) { $this-&gt;response-&gt;body($this-&gt;template-&gt;main_view-&gt;render()); } // Only if auto_render is still TRUE (Default) if ($this-&gt;auto_render === TRUE AND $this-&gt;request-&gt;is_initial()) { // ... more unrelated code here ... // ... get stuff from the database to populate the template ... // now render the response body $this-&gt;response-&gt;body($this-&gt;template-&gt;render()); } // ... more unrelated code here... // including setting headers to disable/enable caching } </code></pre> <p>}</p> <p>And here's <em>an example</em> of what one of the controllers looks like:</p> <pre><code>class Controller_Events extends Controller_Website { public function action_filtered() { // ... do some stuff ... // and set some values $this-&gt;page_info-&gt;page_title = 'Set in Events controller'; // and some more stuff including generating some output } } </code></pre> <p>Now I want one of my other controllers to be able to pull the output from the events controller, without the template. Controller_Website (above) takes care of excluding the template from the output, but consider this:</p> <pre><code>class Controller_Search extends Controller_Website { public function action_index() { // ... do some stuff ... // now let's include the result from our events controller $this-&gt;template-&gt;main_view-&gt;events = Request::factory() -&gt;controller('events') -&gt;action('filtered') -&gt;execute(); // and set some values $this-&gt;page_info-&gt;page_title = 'Set in Search controller'; // and some more stuff including generating some output } } </code></pre> <p>So when my template calls <code>echo $this-&gt;page_info-&gt;page_title;</code> (remember, my template is only being included in the search controller's output and not the event controller's output), I'm expecting it to return <em>"Set in Search controller"</em> but instead it returns <em>"Set in Events Controller"</em></p> <p>The problem is that this <code>action_filtered()</code> method is very long and I've set up a couple routes that use this method to output several event pages (like filtering events by year, month, venue, city, etc.) so it doesn't make sense to duplicate this method in my search controller. Hence the need for an HMVC request. When the filtered action is called as a main/initial request, it makes sense to set values in <code>$page_info</code> but when it's called as a sub-request, I need to preserve the values set in the search controller, or whatever the initial controller is.</p> <p>Of course, I could create an if statement in the events controller to only update these values if it's a main request, but that's less than ideal, obviously. There must be a way to make this sub-request run isolated from the initial request?</p> <p>What am I doing wrong, or what's the best way to go about solving this?</p> <p>Thanks in advance!</p> <p>DM</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload