このチュートリアルは、Symfonyコンポーネントを使用して最小限のPHPフレームワークを構築することを示しています。 網羅的ではありませんが、機能アプリケーションのコア要素をカバーします。 より深いダイブについては、公式のSymfony文書に相談してください
このチュートリアルは、柔軟なフレームワークを構築するためにSymfonyコンポーネントを活用します。 HTTPFoundationを使用して、標準のPHPグローバルを置き換えて、HTTPリクエストと応答を管理します。 ルーティングコンポーネントは動的なURL処理を可能にし、EventDispatcherコンポーネントはオブザーバーパターンを介してモジュール性と拡張性を促進します。 最後に、httpkernelコンポーネントは要求の処理と応答の生成を合理化します。
プロジェクトのセットアップ:基本ファイルから始めて、コンポーザーを使用して必要なコンポーネントをインストールします:
index.php
httpfoundation:
php composer.phar require symfony/http-foundation symfony/http-kernel symfony/routing symfony/event-dispatcher
httpfoundationはおよびクラスを提供します。 最初に、
は(グローバルを使用して)次のように見えるかもしれません:
Request
Response
これは、httpfoundation:index.php
を使用して改善されます
switch($_SERVER['PATH_INFO']) { case '/': echo 'Home'; break; case '/about': echo 'About'; break; default: echo 'Not Found!'; }
httpkernel:
require 'vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $request = Request::createFromGlobals(); $response = new Response(); switch ($request->getPathInfo()) { case '/': $response->setContent('Home'); break; case '/about': $response->setContent('About'); break; default: $response->setContent('Not Found!')->setStatusCode(Response::HTTP_NOT_FOUND); } $response->send();
クラス(例えば、)を作成する:
updateCore
:lib/Framework/Core.php
<?php namespace Framework; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; class Core implements HttpKernelInterface { public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { switch ($request->getPathInfo()) { case '/': return new Response('Home'); case '/about': return new Response('About'); default: return new Response('Not Found!', Response::HTTP_NOT_FOUND); } } }
ルーティングの改善(ルーティングコンポーネント):index.php
require 'lib/Framework/Core.php'; $request = Request::createFromGlobals(); $app = new Framework\Core(); $response = $app->handle($request); $response->send();
クラスが強化されます。 ルートは、
:で定義されています
Core
<?php // ... (previous code) ... use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Exception\ResourceNotFoundException; class Core implements HttpKernelInterface { protected $routes; public function __construct() { $this->routes = new RouteCollection(); } public function handle(Request $request) { $context = new RequestContext(); $context->fromRequest($request); $matcher = new UrlMatcher($this->routes, $context); try { $attributes = $matcher->match($request->getPathInfo()); $controller = $attributes['controller']; unset($attributes['controller']); return call_user_func_array($controller, $attributes); } catch (ResourceNotFoundException $e) { return new Response('Not Found!', Response::HTTP_NOT_FOUND); } } public function map($path, $controller) { $this->routes->add($path, new Route($path, ['controller' => $controller])); } }
index.php
$app->map('/', function() { return new Response('Home'); }); $app->map('/about', function() { return new Response('About'); }); // ...
クラスとクラスに追加します。 (実装の詳細は簡潔に省略されていますが、元の入力の例と同様)。 リスナーは、。
を使用して追加できますこのフレームワークは、Symfonyのパワーと柔軟性を使用して、より複雑なアプリケーションを構築するための基盤を提供します。 より高度な機能とコンポーネントの詳細については、公式のSymfonyドキュメントを参照してください。
以上がSymfonyコンポーネントで独自のPHPフレームワークを構築しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。