本文整理汇总了PHP中Slim\Slim::router方法的典型用法代码示例。如果您正苦于以下问题:PHP Slim::router方法的具体用法?PHP Slim::router怎么用?PHP Slim::router使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Slim
的用法示例。
在下文中一共展示了Slim::router方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _handleEndpointCall
/**
* @param object $endpoint
* @param string $type
* @param array $params
*
* @throws Slim\Exception\Stop
*/
private function _handleEndpointCall($endpoint, $type, array $params)
{
if ($endpoint instanceof SlimBootstrap\Endpoint\InjectClientId) {
$endpoint->setClientId($this->_app->router()->getCurrentRoute()->getParam('clientId'));
}
try {
$outputWriter =& $this->_hook->getResponseOutputWriter();
if ($endpoint instanceof SlimBootstrap\Endpoint\ForceDefaultMimeType) {
$csvConfig = array();
if (true === \array_key_exists('csv', $this->_applicationConfig) && true === \is_array($this->_applicationConfig['csv'])) {
$csvConfig = $this->_applicationConfig['csv'];
}
// create output writer
$responseOutputWriterFactory = new SlimBootstrap\ResponseOutputWriter\Factory($this->_app->request, $this->_app->response, $this->_app->response->headers, $this->_applicationConfig['shortName'], $csvConfig);
$outputWriter = $responseOutputWriterFactory->create($endpoint->getDefaultMimeType());
}
if ($endpoint instanceof SlimBootstrap\Endpoint\Streamable) {
if ($outputWriter instanceof SlimBootstrap\ResponseOutputWriterStreamable) {
$endpoint->setOutputWriter($outputWriter);
\ob_start();
$endpoint->{$type}($params, $this->_app->request->{$type}());
\ob_end_clean();
} else {
throw new SlimBootstrap\Exception('media type does not support streaming', 406, Slim\Log::WARN);
}
} else {
$data = $endpoint->{$type}($params, $this->_app->request->{$type}());
if ($endpoint instanceof SlimBootstrap\Endpoint\PlainData) {
if ($outputWriter instanceof SlimBootstrap\ResponseOutputWriterPlainData) {
$outputWriter->writePlain($data);
} else {
throw new SlimBootstrap\Exception('media type does not support plain data writing', 406, Slim\Log::WARN);
}
} else {
$outputWriter->write($data);
}
}
} catch (SlimBootstrap\Exception $e) {
$this->_app->getLog()->log($e->getLogLevel(), $e->getCode() . ' - ' . $e->getMessage());
$this->_app->response->setStatus($e->getCode());
$this->_app->response->setBody($e->getMessage());
$this->_app->stop();
}
}
示例2: outputError
/**
* Output a generic error page
*
* @param string $status The HTTP status code (eg 401, 404, 500)
* @param string $title Optional brief title of the page, used in HTML head etc
* @param string $summary Optional summary message describing the error
* @param string $extendedTitle Optional extended title, used at top of main content
* @param string $extendedDetails Optional extended details, conveying more details
*
* @throws \InvalidArgumentException if the status is not a valid HTTP status code
*/
protected function outputError($status, $title = null, $summary = '', $extendedTitle = '', $extendedDetails = '')
{
if (!is_integer($status)) {
throw new \InvalidArgumentException('The $status parameter must be a valid integer HTTP status code');
}
$this->app->response->setStatus($status);
// slim does not set the response code in this function
// setting it manually
if (!headers_sent()) {
http_response_code($status);
// PHP 5.4+
}
if (is_null($title)) {
$title = 'Error ' . $status;
}
if ($extendedTitle === '') {
$defaultMsg = \Slim\Http\Response::getMessageForCode($status);
if ($defaultMsg !== null) {
$extendedTitle = substr($defaultMsg, 4);
} else {
$extendedTitle = $title;
}
}
// Content negotiation for the error message
// callbackCheckFormats() middleware does the content negotiation.
// But it was not executed, yet. Call it now to get the mime type.
$route = $this->app->router()->getCurrentRoute();
if ($route) {
$this->mimeBest = $this->callbackCheckFormats($route);
}
// if (!$this->store) {
// $this->store = $route->getParam('store');
// }
// display name of store in titlebar
if (isset($this->storeOptions[$this->store]['shortName'])) {
$u = $this->app->request()->getRootUri() . '/datasets/' . $this->store;
$this->app->view()->set('titleSupplementary', '<a href="' . htmlspecialchars($u, ENT_QUOTES) . '" class="navbar-brand supplementary">' . htmlspecialchars($this->storeOptions[$this->store]['shortName']) . '</a>');
}
switch ($this->mimeBest) {
case 'text/plain':
$error = "status => {$status}" . PHP_EOL;
$error .= "title => {$extendedTitle}" . PHP_EOL;
if ($summary) {
$error .= "summary => {$summary}" . PHP_EOL;
}
if ($extendedDetails) {
$error .= "details => " . preg_replace('~[\\n]+|[\\s]{2,}~', ' ', $extendedDetails);
}
// setBody does not work in this function
$this->app->response->setBody($error);
// render a blank template instead
// $this->app->render('blank.twig', [ 'content' => $error ] );
break;
case 'text/csv':
case 'text/tab-separated-values':
if ($this->mimeBest === 'text/csv') {
$delimiter = ',';
} else {
$delimiter = "\t";
}
$error = [array("status", $status), array("title", $extendedTitle)];
if ($summary) {
$error[] = array("summary", $summary);
}
if ($extendedDetails) {
$error[] = array("details", preg_replace('~[\\n]+|[\\s]{2,}~', ' ', $extendedDetails));
}
ob_start();
$out = fopen('php://output', 'w');
foreach ($error as $err) {
fputcsv($out, $err, $delimiter);
}
fclose($out);
$out = ob_get_contents();
ob_end_clean();
// setBody does not work in this function
$this->app->response->setBody($out);
// render a blank template instead
// $this->app->render('blank.twig', [ 'content' => $out ] );
break;
case 'application/rdf+xml':
case 'application/x-turtle':
case 'text/turtle':
case 'application/json':
$json_error = array('status' => $status, 'title' => $extendedTitle);
if ($summary) {
$json_error['summary'] = $summary;
}
if ($extendedDetails) {
//.........这里部分代码省略.........
示例3: function
$data['metas']['title'] = 'Internal server error';
$app->render('500', $data);
});
// ==================================================================
//
// Cookies advise
//
// ------------------------------------------------------------------
$data['cookieState'] = !isset($_COOKIE[$data['cookies']['name']]) ? true : false;
// ==================================================================
//
// Add before.dispatch and run app
//
// ------------------------------------------------------------------
$app->hook('slim.before.dispatch', function () use($app, $data, $pages) {
$routeName = $app->router()->getCurrentRoute()->getName();
if (isset($data['langs']['metas'][$routeName])) {
$data['metas']['title'] = $data['langs']['metas'][$routeName]['title'];
$data['metas']['description'] = $data['langs']['metas'][$routeName]['description'];
$data['metas']['keywords'] = $data['langs']['metas'][$routeName]['keywords'];
}
$menu_langs = [];
if ($routeName) {
foreach ($data['app_langs'] as $lang) {
$routeItem = new stdClass();
$routeItem->lang = $lang;
$routeItem->route = '/' . $lang;
if (isset($pages[$routeName])) {
if ($lang == $data['default_lang']) {
$routeItem->route = '';
if ($pages[$routeName][$lang]['route'] == '/') {