Skip to content

Instantly share code, notes, and snippets.

@samdark
Created August 3, 2011 22:36
Show Gist options
  • Save samdark/1124018 to your computer and use it in GitHub Desktop.
Save samdark/1124018 to your computer and use it in GitHub Desktop.
Yii: Simple HMVC
<?php
/**
* Yii, simple HMVC
*/
class HmvcController extends Controller
{
public function actionIndex()
{
echo $this->execute('/hmvc/do/id/123');
}
/**
* Processes the request using another controller action and return output.
*
* @param string $route the route of the new controller action. This can be an action ID, or a complete route
* with module ID (optional in the current module), controller ID and action ID. If the former, the action is assumed
* to be located within the current controller.
* @return string output
*/
public function execute($route)
{
$get = $_GET;
ob_start();
$this->forward($route, false);
$out = ob_get_clean();
$_GET = $get;
return $out;
}
public function actionDo($id)
{
echo 'actionDo #'.$id;
}
}
@intel352
Copy link

Nice example @samdark. How about an addition where execute() checks the $route to determine if a local vs remote request? So that you could easily update your $route to a full URL, at which point a full HTTP request is made against the end point, in the event that code which was previously executed within the same app instance, gets migrated to a separate server for scaling concerns.

Similar to the approach discussed here (Kohana/HMVC): http://techportal.inviqa.com/2010/02/22/scaling-web-applications-with-hmvc/

@doonfrs
Copy link

doonfrs commented Aug 20, 2013

good , but what about ajax calls requested by "/hmvc/do/id/123" ??
HmvcController will swallow them ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment