Fuel Documentation

HMVC requests

HMVC requests are a great way to separate logic and re-use controller logic in multiple places. One common use of this is when you use a theme or template engine to generate your pages, where every page is divided into sections, and sections are populated by widgets. By using modules to produce the widget output, you can create a highly modular application, with easy to re-use components.

You call a module controller method using the Request class:

// fetch the output of a controller
$widget = Request::factory('mycontroller/mymethod/parms')->execute();
echo $widget;

// or fetch the output of a module
$widget = Request::factory('mymodule/mycontroller/mymethod/parms', false)->execute();
echo $widget;

By default, all requests made are processed by Fuel's routing engine. If you want to request something that isn't routeable (for example because you don't want a browser to request it) you should pass 'false' as second parameter to the factory() call. If you don't you will end up with a 404 error when you execute the request!

Loading views in HMVC request is the same as in normal requests and controller accessed by HMVC request are also approuchable via the browser. In some cases, for example widgets, are not suppose to accessed through the browser. In those cases you'll want to check if the request is made through HMVC or the browser. The following example shows you how to check for a HMVC request:

<?php

class Controller_Widget extends Controller {

	public function action_show()
	{
		if(Request::main() === Request::active())
		{
			// this is the main request
		}
		else
		{
			// this is a HMVC request
		}
	}

}

404 during HMVC requests

When a 404 occurs during a HMVC request a Request404Exception is thrown. If uncaught it will trigger a full 404 message using Request::show_404(). But you can prevent this and handle the 404 yourself:

try
{
	\Request::factory('this_will_fail');
}
catch (Request404Exception $e)
{
	// deal with it
}

Traversing Request instances

If you want to access other Requests you can traverse them using two methods: $request->parent() and $request->children. The parent is the Request during which the current Request was created (null for the main Request). The children are all the Requests created during the current Request.