LoginSignup
1
0

More than 5 years have passed since last update.

Symfony2 でHTTPステータスコードと表示内容をいい感じにしたい

Posted at

メンテ中とかでメンテ中です画面とか出したいんだけど、メンテ中画面は503がいいらしいんですよね

でも既存の503ページは「一時的に見れません」とか書いてるしメンテ中表示とはちょっと違うし
.htaccessとかいじるのも面倒だし、そもそもSymfony2だからディレクトリ切った下に.htaccess追加すればいいとか言う感じでもないし、どっちにしてもAction内でIPみてこちゃこちゃやる感じだよね云々

public function indexAction () { 
  // 503を返すレスポンス
  return new Responce('メンテナンス中でーす', 503);
}

これだけでもいいんですけどちょっと商業では現実的じゃないよね

public function indexAction () { 

  $frontRendererService = $this->container->get('front_renderer_service');
  $frontRendererService->addExternalTemplateLoader();

  $view = 'maintenance503.twig.html';
  $responce = new Responce('', 503);

  return $this->container->get('templating')->renderResponse($view, [], $response);
}

こんなかんじにすると好きなテンプレートで好きなステータスコードを返せる

public function render($view, array $params = array(), Response $response = null)
{
  $frontRendererService = $this->container->get('front_renderer_service');
  $frontRendererService->addExternalTemplateLoader();
  return $this->container->get('templating')->renderResponse($view, $params, $response);
}

みたいに関数化しとくと、色々な用途で便利に使える

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0