Created
August 3, 2011 22:36
-
-
Save samdark/1124018 to your computer and use it in GitHub Desktop.
Yii: Simple HMVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
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
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/