Skip to content

Commit 7fde34c

Browse files
committed
もうすこしすすんだ
1 parent 75da098 commit 7fde34c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

best_practices/controllers.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,129 @@ Symfonyは薄いコントローラとファットなモデルという哲学に
7171
ほとんどの場合、``@Template``はパラメータの設定をせず使用されます。そして設定すると、どの
7272
テンプレートを表示するかをわかりづらくします。それはまた、必ずコントローラはレスポンスオブジェクトを
7373
返すべきだと言う事を初心者にわかりにくくしてしまいます。(あなたがビューレイヤーを利用している場合以外)
74+
75+
最後に、@Templateアノテーションはkernel.viewイベントのイベントディスパッチのフックとして
76+
TemplateListenerクラスで利用されます。そのリスナーのパフォーマンスへの影響を紹介します。
77+
ブログのサンプルアプリケーションのホームページをレンダリングする場合、$this->render()メソッドを
78+
使った場合5ミリ秒かかり、@Templateアノテーションを使った場合26ミリ秒かかりました。
79+
80+
How the Controller Looks
81+
------------------------
82+
83+
Considering all this, here is an example of how the controller should look
84+
for the homepage of our app:
85+
86+
.. code-block:: php
87+
88+
namespace AppBundle\Controller;
89+
90+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
91+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
92+
93+
class DefaultController extends Controller
94+
{
95+
/**
96+
* @Route("/", name="homepage")
97+
*/
98+
public function indexAction()
99+
{
100+
$em = $this->getDoctrine()->getManager();
101+
$posts = $em->getRepository('App:Post')->findLatest();
102+
103+
return $this->render('default/index.html.twig', array(
104+
'posts' => $posts
105+
));
106+
}
107+
}
108+
109+
.. _best-practices-paramconverter:
110+
111+
ParamConverterを使う
112+
------------------------
113+
114+
もしDoctrineを使っている場合は必要に応じて`ParamConverter`_ を使い、自動的にエンティティを取得し、
115+
コントローラの引数として渡す必要があります。
116+
117+
.. best-practice::
118+
119+
シンプルかつ簡単な場合は、自動的にDoctrineのエンティティを取得出来るParamConverterを使用
120+
してください。
121+
122+
例:
123+
124+
.. code-block:: php
125+
126+
/**
127+
* @Route("/{id}", name="admin_post_show")
128+
*/
129+
public function showAction(Post $post)
130+
{
131+
$deleteForm = $this->createDeleteForm($post);
132+
133+
return $this->render('admin/post/show.html.twig', array(
134+
'post' => $post,
135+
'delete_form' => $deleteForm->createView(),
136+
));
137+
}
138+
139+
通常は ``showAction`` では ``$id`` という変数を引数として使うと思います。
140+
代わりに ``$post`` 引数と ``Post`` クラス(Doctrineのエンティティ)をタイプヒンティングする
141+
ことによって、そのオブジェクトを自動的にParamConverterが``{id}`` の値と一致する
142+
``$id`` プロパティのものを取得します。``Post`` が見つからなかった場合は404ページが表示されます。
143+
144+
高度な事
145+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146+
147+
This works without any configuration
148+
This works without any configuration because the wildcard name ``{id}`` matches
149+
the name of the property on the entity. If this isn't true, or if you have
150+
even more complex logic, the easiest thing to do is just query for the entity
151+
manually. In our application, we have this situation in ``CommentController``:
152+
153+
.. code-block:: php
154+
155+
/**
156+
* @Route("/comment/{postSlug}/new", name = "comment_new")
157+
*/
158+
public function newAction(Request $request, $postSlug)
159+
{
160+
$post = $this->getDoctrine()
161+
->getRepository('AppBundle:Post')
162+
->findOneBy(array('slug' => $postSlug));
163+
164+
if (!$post) {
165+
throw $this->createNotFoundException();
166+
}
167+
168+
// ...
169+
}
170+
171+
You can also use the ``@ParamConverter`` configuration, which is infinitely
172+
flexible:
173+
174+
.. code-block:: php
175+
176+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
177+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
178+
179+
/**
180+
* @Route("/comment/{postSlug}/new", name = "comment_new")
181+
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
182+
*/
183+
public function newAction(Request $request, Post $post)
184+
{
185+
// ...
186+
}
187+
188+
The point is this: the ParamConverter shortcut is great for simple situations.
189+
But you shouldn't forget that querying for entities directly is still very
190+
easy.
191+
192+
Pre and Post Hooks
193+
------------------
194+
195+
If you need to execute some code before or after the execution of your controllers,
196+
you can use the EventDispatcher component to `set up before/after filters`_.
197+
198+
.. _`ParamConverter`: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
199+
.. _`set up before/after filters`: http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html

0 commit comments

Comments
 (0)