本文整理汇总了PHP中Silex\Application::error方法的典型用法代码示例。如果您正苦于以下问题:PHP Application::error方法的具体用法?PHP Application::error怎么用?PHP Application::error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Silex\Application
的用法示例。
在下文中一共展示了Application::error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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['error.controller'] = $controller = new ErrorController($app);
$app->error([$controller, 'onApiProblemException']);
$app->error([$controller, 'whenNotFound']);
$app->error([$controller, 'whenNotAllowed']);
$app->error([$controller, 'onHttpException']);
$app->error([$controller, 'onGeneralException']);
}
示例2: setupErrorHandlers
/**
* Setup error handlers
*/
protected function setupErrorHandlers()
{
$this->app->error(function (\Exception $e, $code) {
$data = array('status' => $code);
if ($this->app['config']('app.debug', false)) {
$data['error'] = $e->getMessage();
}
return $this->app->json($data, $code);
});
}
示例3: register
/**
* @inheritdoc
*/
public function register(Application $app)
{
$provider = $this;
$app->error(function (ValidationException $e) use($provider) {
$problem = new ApiProblem($e->getMessage());
$problem['validation_messages'] = $e->getErrors();
return $provider->createBadRequestResponse($problem);
});
$app->error(function (InvalidNativeArgumentException $e) use($provider) {
$problem = new ApiProblem($e->getMessage());
return $provider->createBadRequestResponse($problem);
});
}
示例4: defineErrorHandler
/**
* Define error handler
*/
private function defineErrorHandler()
{
$this->app->error(function (\Exception $e, $code) {
if ($this->app['debug']) {
return null;
}
switch ($code) {
case 404:
return ResponseBuilder::createErrorResponse($this->app, ResponseBuilder::STATUS_TYPE_ROUTE_NOT_FOUND, $e->getMessage(), $code);
default:
return ResponseBuilder::createErrorResponse($this->app, ResponseBuilder::STATUS_TYPE_ERROR, $e->getMessage(), $code);
}
});
}
示例5: boot
public function boot(Application $app)
{
$app->error(function (Exception $e) use($app) {
$app['logger']->addError($e->getMessage());
return new Response($e->getMessage());
});
}
示例6: __construct
public function __construct(array $config = array())
{
parent::__construct($config);
parent::error(function (\Exception $e, $code) {
return parent::json(array("error" => $e->getMessage()), $code);
});
}
示例7: connect
public function connect(Application $app)
{
$route = $app['controllers_factory'];
$route->get('/', function (Application $app, Request $request) {
return $app['mh.controllers.base']->index($request);
});
$route->post('/guest', function (Application $app, Request $request) {
return $app['mh.controllers.guest']->find($request);
});
$route->get('/pay/{id}', function (Application $app, Request $request, $id) {
return $app['mh.controllers.guest']->payment($id);
});
$route->post('/process/{id}', function (Application $app, Request $request, $id) {
return $app['mh.controllers.transaction']->pay($request, $id);
});
$route->get('/not-found', function (Application $app) {
return $app['mh.controllers.guest']->notFound();
});
$route->get('/thank-you', function (Application $app) {
return $app['mh.controllers.guest']->thanks();
});
$app->error(function (\Exception $e, $code) {
if (404 == $code) {
return new Response('', 301, ['location' => '/']);
}
});
return $route;
}
示例8: boot
public function boot(\Silex\Application $app)
{
$app->before(function (\Symfony\Component\HttpFoundation\Request $request) use($app) {
$routeName = $request->attributes->get('_route');
$route = $request->getRequestUri();
$log = $_SERVER['REMOTE_ADDR'] . ' - ';
if ($app['session']->has('user') && defined('USERNAME_METHOD_LOGGED')) {
$user = $app['session']->get('user');
$method = USERNAME_METHOD_LOGGED;
if (is_callable(array($user, $method))) {
$name = $user->{$method}();
if (!empty($name)) {
$log .= $name;
}
}
}
if (!empty($routeName)) {
$log .= ' está acessando a rota "' . $routeName . '" (' . $route . ')';
} else {
if (!file_exists(__WEBROOT__ . $route)) {
$log .= ' tentou acessar um arquivo ou rota inexistente (' . $route . ')!';
} else {
$log .= ' está acessando um arquivo (' . $route . ')!';
}
}
$app['monolog']->addInfo($log);
});
$app->error(function (\Exception $e, $code) use($app) {
$msg = $code != 500 ? $e->getMessage() : $e->getFile() . ' na linha ' . $e->getLine() . ': ' . $e->getMessage();
$app['monolog']->addError('cod: ' . $code . ' => ' . $msg);
});
}
示例9: boot
public function boot(Application $app)
{
$app->before(function (Request $request) use($app) {
$app['monolog']->addInfo('> ' . $request->getMethod() . ' ' . $request->getRequestUri());
});
/*
* Priority -4 is used to come after those from SecurityServiceProvider (0)
* but before the error handlers added with Silex\Application::error (defaults to -8)
*/
$app->error(function (\Exception $e) use($app) {
$message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
if ($e instanceof HttpExceptionInterface && $e->getStatusCode() < 500) {
$app['monolog']->addError($message, array('exception' => $e));
} else {
$app['monolog']->addCritical($message, array('exception' => $e));
}
}, -4);
$app->after(function (Request $request, Response $response) use($app) {
if ($response instanceof RedirectResponse) {
$app['monolog']->addInfo('< ' . $response->getStatusCode() . ' ' . $response->getTargetUrl());
} else {
$app['monolog']->addInfo('< ' . $response->getStatusCode());
}
});
}
示例10: testRegisterAndRender
public function testRegisterAndRender()
{
$app = new Application();
$app->register(new MonologExtension(), array('monolog.class_path' => __DIR__ . '/../../../../vendor/monolog/src'));
$app['monolog.handler'] = $app->share(function () use($app) {
return new TestHandler($app['monolog.level']);
});
$app->get('/log', function () use($app) {
$app['monolog']->addDebug('logging a message');
});
$app->get('/error', function () {
throw new \RuntimeException('very bad error');
});
$app->error(function (\Exception $e) {
return 'error handled';
});
$this->assertFalse($app['monolog.handler']->hasDebugRecords());
$this->assertFalse($app['monolog.handler']->hasErrorRecords());
$request = Request::create('/log');
$app->handle($request);
$request = Request::create('/error');
$app->handle($request);
$this->assertTrue($app['monolog.handler']->hasDebugRecords());
$this->assertTrue($app['monolog.handler']->hasErrorRecords());
}
示例11: createApplication
/**
* Bootstrap Silex Application for tests
* @method createApplication
* @return $app Silex\Application
*/
public function createApplication()
{
$app = new Application(['dev' => true]);
// errors
error_reporting(E_ALL ^ E_STRICT);
$app->error(function (Exception $e, $code) use($app) {
return $app->json(['error' => $e->getMessage(), 'type' => get_class($e)], $code);
});
// database
$app->register(new DoctrineServiceProvider(), ['db.options' => ['driver' => 'pdo_sqlite', 'path' => __DIR__ . '/tests.db', 'charset' => 'UTF8']]);
// jwt (json-web-token)
$app['security.jwt'] = ['secret_key' => 'omg-so-secret-test-!', 'life_time' => 2592000, 'algorithm' => ['HS256'], 'options' => ['header_name' => 'X-Access-Token', 'username_claim' => 'email']];
$app->register(new SecurityServiceProvider());
$app->register(new SecurityJWTServiceProvider());
// mailer
$app->register(new SwiftmailerServiceProvider(), ['swiftmailer.options' => ['host' => '127.0.0.1', 'port' => '1025']]);
// twig
$app->register(new TwigServiceProvider(), ['twig.path' => __DIR__ . '/../templates']);
$app->register(new UrlGeneratorServiceProvider());
// simple-user-jwt
$app['user.jwt.options'] = ['invite' => ['enabled' => true], 'forget' => ['enabled' => true], 'mailer' => ['enabled' => true, 'from' => ['email' => 'do-not-reply@test.com', 'name' => 'Test']]];
$app->register(new UserProvider());
// roles
$app['security.role_hierarchy'] = ['ROLE_INVITED' => ['ROLE_USER'], 'ROLE_REGISTERED' => ['ROLE_INVITED', 'ROLE_ALLOW_INVITE'], 'ROLE_ADMIN' => ['ROLE_REGISTERED']];
// needed to parse user at each request
$app['security.firewalls'] = ['login' => ['pattern' => 'register|login|forget|reset', 'anonymous' => true], 'secured' => ['pattern' => '.*$', 'users' => $app->share(function () use($app) {
return $app['user.manager'];
}), 'jwt' => ['use_forward' => true, 'require_previous_session' => false, 'stateless' => true]]];
// controller
$app->mount('/', new UserProvider());
return $app;
}
示例12: register
public function register(Application $app)
{
$app['monolog'] = $app->share(function () use($app) {
$log = new Logger(isset($app['monolog.name']) ? $app['monolog.name'] : 'myapp');
$app['monolog.configure']($log);
return $log;
});
$app['monolog.configure'] = $app->protect(function ($log) use($app) {
$log->pushHandler($app['monolog.handler']);
});
$app['monolog.handler'] = function () use($app) {
return new StreamHandler($app['monolog.logfile'], $app['monolog.level']);
};
if (!isset($app['monolog.level'])) {
$app['monolog.level'] = function () {
return Logger::DEBUG;
};
}
if (isset($app['monolog.class_path'])) {
$app['autoloader']->registerNamespace('Monolog', $app['monolog.class_path']);
}
$app->before(function (Request $request) use($app) {
$app['monolog']->addInfo('> ' . $request->getMethod() . ' ' . $request->getRequestUri());
});
$app->error(function (\Exception $e) use($app) {
$app['monolog']->addError($e->getMessage());
});
$app->after(function (Request $request, Response $response) use($app) {
$app['monolog']->addInfo('< ' . $response->getStatusCode());
});
}
示例13: connect
public function connect(Application $app)
{
# bind app to his controller
$this->app = $app;
# bind errro handler
$app->error(array($this, 'handleError'));
}
示例14: setJsonErrorHandler
protected function setJsonErrorHandler()
{
/** @noinspection PhpUnusedParameterInspection */
$this->app->error(function (\Exception $e, $code) {
$message = 'Threw ' . get_class($e) . ': ' . $e->getMessage();
return new JsonResponse(['error' => $message]);
});
}
示例15: boot
public function boot(Application $app)
{
$app->error(function (Exception $e) use($app) {
$app['logger']->addError($e->getMessage());
$status = $e->getCode() > 99 ? $e->getCode() : 500;
return new Response($e->getMessage(), $status);
});
}