Symfony2に基づいて独自のフレームワークを作成します。 (パート5)

シリーズの第5部では、コントローラーについて説明します。







気配りのある読者は、私たちのフレームワークが、テンプレートコードが実行される厳密に綴られた方法を持っていることに気付きました。 前に作成したような単純なページの場合、これは問題ではありません。 ただし、さらにロジックを追加する場合は、テンプレート自体に埋め込む必要があります。



もちろん、システムを個別の機能構造に分割する場合は特に、これはお勧めできません。

新しいレイヤー-コントローラーを追加して、テンプレートコードをロジックから分離しましょう。「コントローラーのタスクは、クライアントのリクエストによって送信された情報に基づいて応答を作成することです。」

レンダリングを担当するテンプレートの一部を次のように変更します。



<?php // example.com/web/front.php // ... try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func('render_template', $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); }
      
      







レンダリングは外部のrender_template()関数によって行われるようになったため、URLから抽出した属性を渡す必要があります。 render_template()に追加の引数として渡すこともできますが、代わりにRequestクラスの別の機能である属性を使用します。 リクエスト属性を使用すると、リクエストに関する追加情報を含めることができます。これは、HTTPリクエストのデータに直接関連していません。

次に、テンプレートをレンダリングする汎用コントローラーであるrender_template()関数を、あまりロジックなしで作成します。 テンプレートの古い構造を保持するために、テンプレートがレンダリングされる前にクエリ属性が取得されます。



 function render_template($request) { extract($request->attributes->all(), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); return new Response(ob_get_clean()); }
      
      







render_templateはPHP call_user_func()の引数として使用されるため、有効なPHPコールバック関数に置き換えることができます。 これにより、選択した関数、匿名関数、またはコントローラークラスメソッドを使用できます。

パスの定義を標準化するために、関連するコントローラーは_controller route属性を使用して構成されます。



 $routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => 'render_template', ))); try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func($request->attributes->get('_controller'), $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); }
      
      







ルートは任意のコントローラーに接続できるようになりました。もちろん、コントローラーでは、 render_template()を使用してテンプレートをレンダリングできます。



 $routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => function ($request) { return render_template($request); } )));
      
      







Responseオブジェクトを変更できるため、これはより柔軟です。テンプレートに追加の引数を渡すこともできます。



 $routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => function ($request) { // $foo will be available in the template $request->attributes->set('foo', 'bar'); $response = render_template($request); // change some header $response->headers->set('Content-Type', 'text/plain'); return $response; } )));
      
      







システムの更新および改善されたバージョンは次のとおりです。



 <?php // example.com/web/front.php require_once __DIR__.'/../vendor/.composer/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing; function render_template($request) { extract($request->attributes->all(), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); return new Response(ob_get_clean()); } $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/app.php'; $context = new Routing\RequestContext(); $context->fromRequest($request); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func($request->attributes->get('_controller'), $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send();
      
      







フレームワークの誕生日を記念して、最も単純なロジックを持つ新しいアプリケーションを作成しましょう。 私たちのアプリケーションは、ある特定の年がうるう年かどうかという1ページで構成されています。 / is_leap_yearを呼び出すと、現在の年に対する回答が得られますが、年を/ is_leap_year / 2009の形式で指定することもできます。 普遍的であるため、フレームワークを変更する必要はなく、新しいapp.phpファイルを作成するだけです:



 <?php // example.com/src/app.php use Symfony\Component\Routing; use Symfony\Component\HttpFoundation\Response; function is_leap_year($year = null) { if (null === $year) { $year = date('Y'); } return 0 == $year % 400 || (0 == $year % 4 && 0 != $year % 100); } $routes = new Routing\RouteCollection(); $routes->add('leap_year', new Routing\Route('/is_leap_year/{year}', array( 'year' => null, '_controller' => function ($request) { if (is_leap_year($request->attributes->get('year'))) { return new Response('Yep, this is a leap year!'); } return new Response('Nope, this is not a leap year.'); } ))); return $routes;
      
      







Is_leap_year()は、指定された年がうるう年の場合はtrueを返し、そうでない場合はfalseを返します。 年がnullの場合、現在の年がチェックされます。 コントローラーはシンプルです。リクエストの属性から年を受け取り、 is_leap_year()渡し 、戻り値に応じて新しいResponseオブジェクトを作成します。

いつものように、そこで停止し、フレームワークをそのまま使用することを決定できます。 これはおそらく、単純な1ページのサイトを作成するのに十分であり、場合によっては2ページ以上のサイトでも作成できます。



All Articles