Created
October 23, 2012 13:35
-
-
Save toopay/3938765 to your computer and use it in GitHub Desktop.
Action controller with arguments proxy
This file contains hidden or 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
//... | |
/** | |
* Serve HTTP request | |
* | |
* @param string | |
* @param string | |
* @return response | |
*/ | |
public function executeHttp($proxy = 'Home', $method = 'index', $id = NULL) | |
{ | |
$currentRequest = $this->getRequest(); | |
// Check symlink socket | |
if (count($currentRequest->getPath()) === 1 && count($currentRequest->getArguments()) === 0) { | |
$proxy = current($currentRequest->getPath()); | |
} | |
$actionClass = __NAMESPACE__ . '\\Controller\\' . ucfirst($proxy) . 'Controller'; | |
if ($actionClass == get_class($this)) { | |
$arguments = $currentRequest->getArguments(); | |
if (empty($arguments)) { | |
$method = 'index'; | |
} elseif (count($arguments) == 1) { | |
$method = current($arguments); | |
} | |
} | |
if ( !is_callable(array($actionClass, $method . self::ACTION))) { | |
$socket = new ErrorController($this->getRequest()); | |
$content = call_user_func(array($socket, 'Notfound' . self::ACTION)); | |
return new ResponseHttp(404, $content); | |
} | |
// Build content from implementor | |
$socket = new $actionClass($this->getRequest()); | |
$content = call_user_func(array($socket, $method . self::ACTION), $id); | |
// Perform regular HTTP response | |
$response = new ResponseHttp(); | |
$response->setCode(200); | |
$response->setHeaders(array('Content-Type' => 'text/html; charset=utf-8')); | |
$response->setContent($content); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment