本文整理汇总了PHP中Silex\Application::view方法的典型用法代码示例。如果您正苦于以下问题:PHP Application::view方法的具体用法?PHP Application::view怎么用?PHP Application::view使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Silex\Application
的用法示例。
在下文中一共展示了Application::view方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
/**
* Registers services on the given app.
*
* This method should only be used to configure services and parameters.
* It should not get services.
* @param Application $app
*/
public function register(Application $app)
{
$app->view(function (Health $healthResult) use($app) {
$healthDetails = array();
foreach ($healthResult->getDetails() as $key => $healthDetail) {
$healthDetails[$key] = array_merge(array('status' => $healthDetail->getStatus()->getCode()), $healthDetail->getDetails());
}
$healthDetails = array_merge(array('status' => $healthResult->getStatus()->getCode()), $healthDetails);
return $app->json($healthDetails);
});
$app['health.aggregator'] = $app->share(function () {
return new OrderedHealthAggregator();
});
$app['health.indicators'] = array();
$app['health.endpoint'] = '/health';
}
示例2: testViewListenersResponsesAreNotUsedIfNull
public function testViewListenersResponsesAreNotUsedIfNull()
{
$app = new Application();
$app->get('/foo', function () {
return 'Hello world';
});
$app->view(function ($view) {
return 'Hello view listener';
});
$app->view(function ($view) {
return;
});
$response = $app->handle(Request::create('/foo'));
$this->assertEquals('Hello view listener', $response->getContent());
}
示例3: connect
public function connect(Application $app)
{
$app->view(function (array $result, Request $request) use($app) {
return $app->json($result['body'], $result['code'], $result['headers']);
});
$app->before(function (Request $request) {
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
});
if (isset($app['restapi']['cors'])) {
$app->before(function (Request $request, Application $app) {
return $app['restapi.middleware.cors']->processRequest($request);
}, Application::EARLY_EVENT);
$app->after(function (Request $request, Response $response, Application $app) {
return $app['restapi.middleware.cors']->processResponse($request, $response);
});
}
$controllers = $app['controllers_factory'];
if (isset($app['restapi']['auth'])) {
$controllers->post('/auth/login', function (Request $request) use($app) {
if (!$request->request->has('username') or !$request->request->has('password')) {
return new Response(null, 400, ['Content-Type' => 'application/json']);
}
$username = $request->request->get('username');
$password = $request->request->get('password');
if (true !== $app['restapi.auth']->verifyCredentials($username, $password)) {
return new Response(null, 401, ['Content-Type' => 'application/json']);
}
$response = new Response(['username' => $username, 'token' => $app['restapi.auth']->createJwtTokenForUser($username)]);
$response->headers->setCookie($app['restapi.auth']->createCookieForToken($token));
if (true === $request->request->has('redirect')) {
$response->headers->set('Location', $request->request->get('redirect'));
$response->setStatusCode(302);
}
$response->headers->set('Content-Type', 'application/json');
return $response;
});
$controllers->post('/auth/logout', function () use($app) {
$response = new Response(null, 204, ['Content-Type' => 'application/json']);
$cookie = $app['restapi.auth']->deleteCookie();
$response->headers->setCookie($cookie);
return $response;
});
}
$resources = $app['controllers_factory'];
if (isset($app['restapi']['auth'])) {
$resources->before($app['restapi.listener.auth_checker']);
}
$resources->get('/', function () use($app) {
return $app['restapi.service']->listResources();
});
$resources->get('/files/{hash}', function ($hash) use($app) {
return $app->sendFile($app['restapi.service']->fetchFile($hash));
});
$resources->get('/thumbs/{hash}', function ($hash) use($app) {
try {
return $app->sendFile($app['restapi']['thumbs_path'] . '/' . $app['restapi.storage']->hashToFilePath($hash) . '.png');
} catch (\Exception $e) {
return new Response(null, 404);
}
});
$resources->get('/{table}', function (Request $request, $table) use($app) {
return $app['restapi.service']->readCollection($table, $request->query->all());
});
$resources->post('/{table}', function (Request $request, $table) use($app) {
return $app['restapi.service']->createResource($table, array_merge($request->request->all(), $request->files->all()));
});
$resources->get('/{table}/{pk}', function (Request $request, $table, $pk) use($app) {
return $app['restapi.service']->readResource($table, $pk, $request->query->all());
});
$resources->match('/{table}/{pk}', function (Request $request, $table, $pk) use($app) {
return $app['restapi.service']->updateResource($table, $pk, array_merge($request->request->all(), $request->files->all()));
})->method('POST|PATCH');
$resources->delete('/{table}/{pk}', function ($table, $pk) use($app) {
return $app['restapi.service']->deleteResource($table, $pk);
});
$controllers->mount('/', $resources);
return $controllers;
}