本文整理汇总了PHP中Silex\Application::options方法的典型用法代码示例。如果您正苦于以下问题:PHP Application::options方法的具体用法?PHP Application::options怎么用?PHP Application::options使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Silex\Application
的用法示例。
在下文中一共展示了Application::options方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
public function register(Application $app)
{
$app['tba.obj'] = $app->share(function () use($app) {
if (isset($app['config'])) {
$this->config['table_name'] = $app['config']->tba->table_name;
$this->config['user_field'] = $app['config']->tba->user_field;
$this->config['pass_field'] = $app['config']->tba->pass_field;
$this->config['token_timeout'] = $app['config']->tba->token_timeout;
$this->config['salt'] = $app['config']->tba->salt;
} else {
$this->config['table_name'] = $app['tba.table_name'];
$this->config['user_field'] = $app['tba.user_field'];
$this->config['pass_field'] = $app['tba.pass_field'];
$this->config['token_timeout'] = $app['tba.token_timeout'];
$this->config['salt'] = $app['tba.salt'];
}
$tba = new TokenBasedAuth($this->config);
$tba->setHeader(new \TBA\Header());
if (isset($app['db'])) {
$tba->setConnection($app['db']);
}
return $tba;
});
$app->get('/logout', function () {
if (isset($_SESSION["m1_53ck_m4"])) {
unset($_SESSION["m1_53ck_m4"]);
}
$dados = ["loggedOut" => "ok"];
error_log("/logout");
return new JsonResponse($dados, 200);
});
$app->options('/login', function () {
return new JsonResponse(["result" => "ok"], 200);
});
$app->post('/login', function (Request $request) use($app) {
try {
$dados = array();
$form = $request->request->all();
if (!isset($form['user']) || !isset($form['passwd'])) {
error_log("1 - email ou senha não enviados");
throw new UnauthorizedException(self::ERRO_1, 120001);
}
$pwdHash = md5($this->config['salt'] . "{$form['passwd']}123X");
if (!$app['tba.obj']->login($form['user'], $pwdHash)) {
error_log("senha informada não confere: {$form['user']} - {$pwdHash}");
throw new UnauthorizedException("erro no login", 120004);
}
$dados["msg"] = "Login com sucesso";
if (isset($this->config->token_as_field) && $this->config->token_as_field == true) {
$dados['token'] = $app['tba.obj']->getUser()->token;
}
unset($form);
$response = new JsonResponse($dados, 200);
$response->headers->set("ClientToken", $app['tba.obj']->getUser()->token);
return $response;
} catch (UnauthorizedException $e) {
error_log(">> 1 - 401 - erro verificado: {$e->getMessage()}");
return new JsonResponse(["msg" => $e->getMessage()], 401);
} catch (InvalidLoginException $e) {
error_log(">> 1 - 403 - erro verificado: {$e->getMessage()}");
return new JsonResponse(["msg" => $e->getMessage()], 403);
} catch (\Exception $e) {
error_log(">> 2 - 500 - erro verificado: {$e->getMessage()}");
return new JsonResponse(["msg" => $e->getMessage()], 500);
}
});
$app->before(function (Request $request) use($app) {
if (is_null($request->headers->get('AppToken'))) {
error_log("token não informado");
throw new UnauthorizedException("Aplicação não reconhecida", 120006);
}
if ($request->headers->get('AppToken') !== APP_TOKEN) {
error_log("token não confere");
throw new UnauthorizedException("Aplicação não reconhecida", 120005);
}
}, 1);
$app->before(function (Request $request) use($app) {
if (!in_array($request->getRequestUri(), $app['login.openroutes'])) {
$dados = array();
if (is_null($request->headers->get('ClientToken'))) {
throw new UnauthorizedException("Cliente não reconhecido");
}
}
}, 3);
$app->after(function (Request $request, Response $response) use($app) {
$response->headers->set("AppToken", APP_TOKEN);
try {
$user = $app['tba.obj']->getUserByToken();
error_log(print_r($user, true));
$response->headers->set("ClientToken", $user->token);
} catch (\Exception $e) {
error_log("client token: {$e->getMessage()}");
}
return $response;
}, 1);
}
示例2: function
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
$response->headers->set('Access-Control-Max-Age', '1000');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
});
$app->get('/races', function (Application $app, Request $req) {
$pp = (int) $req->get('_perPage');
$p = (int) $req->get('_page') - 1;
if ($pp == 0) {
$pp = 30;
}
return $app->json(all('select id, name from dogs_race limit ' . $pp . ' offset ' . $p * $pp), 200, ['X-Total-Count' => col('select count(*) from dogs_race')]);
});
$app->options('/races', function () {
return '';
});
$app->post('/races', function (Application $app, Request $req) {
insert('dogs_race', $req->request->all());
return $app->json(one('select * from dogs_race order by id desc limit 1'));
});
$app->get('/races/{id}', function (Application $app, Request $req, $id) {
return $app->json(one('select * from dogs_race where id =' . q($id)));
});
$app->put('/races/{id}', function (Application $app, Request $req, $id) {
$json = $req->request->all();
unset($json['id']);
update('dogs_race', $id, $json);
return $app->json();
});
$app->options('/races/{id}', function (Application $app, Request $req, $id) {
示例3: options
/**
* @param string $pattern
* @param Endpoint $to
* @return \Silex\Controller
*/
public function options($pattern, $to = null)
{
return parent::options($pattern, $to);
}