本文整理汇总了PHP中Presenter::setServiceLocator方法的典型用法代码示例。如果您正苦于以下问题:PHP Presenter::setServiceLocator方法的具体用法?PHP Presenter::setServiceLocator怎么用?PHP Presenter::setServiceLocator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Presenter
的用法示例。
在下文中一共展示了Presenter::setServiceLocator方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Dispatch a HTTP request to a front controller.
*/
public function run()
{
$httpRequest = $this->getHttpRequest();
$httpResponse = $this->getHttpResponse();
$httpRequest->setEncoding('UTF-8');
$httpResponse->setHeader('X-Powered-By', 'Nette Framework');
if (Environment::getVariable('baseUri') === NULL) {
Environment::setVariable('baseUri', $httpRequest->getUri()->basePath);
}
// check HTTP method
$method = $httpRequest->getMethod();
if ($this->allowedMethods) {
if (!in_array($method, $this->allowedMethods, TRUE)) {
$httpResponse->setCode(IHttpResponse::S501_NOT_IMPLEMENTED);
$httpResponse->setHeader('Allow', implode(',', $this->allowedMethods));
$method = htmlSpecialChars($method);
die("<h1>Method {$method} is not implemented</h1>");
}
}
// dispatching
$request = NULL;
$hasError = FALSE;
do {
try {
if (count($this->requests) > self::$maxLoop) {
throw new ApplicationException('Too many loops detected in application life cycle.');
}
if (!$request) {
$this->onStartup($this);
// default router
$router = $this->getRouter();
if ($router instanceof MultiRouter && !count($router)) {
$router[] = new SimpleRouter(array('presenter' => 'Default', 'action' => 'default'));
}
// routing
$request = $router->match($httpRequest);
if (!$request instanceof PresenterRequest) {
$request = NULL;
throw new BadRequestException('No route for HTTP request.');
}
if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
throw new BadRequestException('Invalid request.');
}
}
$this->requests[] = $request;
$this->onRequest($this, $request);
// Instantiate presenter
$presenter = $request->getPresenterName();
try {
$class = $this->getPresenterLoader()->getPresenterClass($presenter);
$request->modify('name', $presenter);
} catch (InvalidPresenterException $e) {
throw new BadRequestException($e->getMessage(), 404, $e);
}
$this->presenter = new $class($request);
// Instantiate topmost service locator
$this->presenter->setServiceLocator(new ServiceLocator($this->serviceLocator));
// Execute presenter
$this->presenter->run();
break;
} catch (RedirectingException $e) {
// not error, presenter redirects to new URL
$httpResponse->redirect($e->getUri(), $e->getCode());
break;
} catch (ForwardingException $e) {
// not error, presenter forwards to new request
$request = $e->getRequest();
} catch (AbortException $e) {
// not error, application is correctly terminated
unset($e);
break;
} catch (Exception $e) {
// fault barrier
if ($this->catchExceptions === NULL) {
$this->catchExceptions = Environment::isProduction();
}
if (!$this->catchExceptions) {
throw $e;
}
$this->onError($this, $e);
if ($hasError) {
$e = new ApplicationException('An error occured while executing error-presenter', 0, $e);
} elseif ($this->errorPresenter) {
$hasError = TRUE;
$request = new PresenterRequest($this->errorPresenter, PresenterRequest::FORWARD, array('exception' => $e));
continue;
}
if ($e instanceof BadRequestException) {
if (!$httpResponse->isSent()) {
$httpResponse->setCode($e->getCode());
}
echo "<title>404 Not Found</title>\n\n<h1>Not Found</h1>\n\n<p>The requested URL was not found on this server.</p>";
break;
} else {
if (!$httpResponse->isSent()) {
$httpResponse->setCode(500);
}
//.........这里部分代码省略.........