Home > Kohana 3 > Kohana 3.2 and Controller/Action forward() functionality

Kohana 3.2 and Controller/Action forward() functionality

I just started a personal project with Kohana 3.2 HMVC, where it made a lot of sense to move my routing logic to a custom “Routing” controller (multi-language site, variable URL tokens based on language, etc.). In my particular scenario, PHP logic is in charge of analyzing the URL segments and forwarding to the “real” Controller/Action that should process the request.

Unfortunately, Kohana 3.2 doesn’t include a “forward()” method in Controllers as most MVC frameworks do (the framework is strict HMVC). It should include the method if you ask me :-)

Being “strict” is good most of the time, in the particular case of Kohana, I think it would be awesome to have that feature available out of the box so we developers can decide on what’s the best approach for our particular needs, this without the need to work on extending the framework.

Anyhow, I ended up creating a small extension, just create the file [APP_PATH]/application/classes/request.php with the following contents:

<?php defined('SYSPATH') or die('No direct script access.');

class Request extends Kohana_Request
{
   /**
    * Kohana doesn's support it because they are strict HMVC
    * .. but I need it for my particular app/routing structure!
    * let's implement it then ;-) 
    */
    public function forward($controller, $action = 'index')
    {
        $this->controller($controller);
        $this->action($action);
        echo $this->execute()
            ->send_headers()
            ->body();
        exit;
    }
}

This extends Kohana’s Request class, adding the forward method, now from your controller you can just:

$this->request->forward('[controller]', '[action]');

This is a quick extension I just made and still testing, but it seems to work fine so far, please let me know if you have any additions/corrections.

Cheers!

  1. Patrick Cummins
    February 4th, 2013 at 14:44 | #1

    Thank for this. It’s a shortcoming in the Kohana core.

  1. No trackbacks yet.