SymfonyのControllerでのResponseの返し方
やたら方法があって難しい
twig templateを返す方法は2種類ある
$this->render('path/to/page.twig', $params)を使う [ref ref]
@Template("path/to/page.twig")を使う
この場合、返り値は$params
code:php
/** @Template("path/to/page.twig") */
function pageAction() {
..
return 'id' => $id;
}
Redirect 3XX
https://symfony.com/doc/3.4/controller.html#redirecting
5種類ぐらいある(?)
Error pageを返す
4XX
予め用意された該当のException classをthrowする ref
例えば、404なら$this->createNotFoundException('...')
これらのclassは、HttpExceptionを継承したclassになる
5XX ref
4XXの以外の方法でthrowしたものは5XXになる
これらの場合は、Error pageが表示される
あの赤いページ
https://gyazo.com/7e9470cc052b761f404379f941f6ec5d
カスタマイズするためには、↓を見る
https://symfony.com/doc/3.4/controller/error_pages.html
$this->json()
JsonResponseのwrapper
引数に渡したものは全てJSONに変換される
#??
直接Responseclassを返すことは殆どない?
methodがActionではなく、APIの場合は?
axiosなど経由で実行されている場合、doneかfailのいずれかに入る
もちろん異常の時(また、その時に限り)、fail()に渡したい
コレに関しては、docsのこのページでは一切言及がない
Response class
https://symfony.com/doc/3.4/controller.html#request-object-info
https://github.com/symfony/symfony/blob/3.4/src/Symfony/Component/HttpFoundation/Response.php
https://symfony.com/doc/3.4/components/http_foundation.html#response
HTTP responseの抽象化