>本教程展示了使用Symfony組件構建最小的PHP框架。 雖然並不詳盡,但它涵蓋了功能應用的核心元素。 有關更深入的潛水,請諮詢官方的Symfony文檔。
密鑰概念:
>本教程利用符號組件來構建靈活的框架。 它使用HTTPFOUNDATION來管理HTTP請求和響應,以取代標準PHP Globals。 該路由組件可以啟用動態URL處理,並且EventDisPatcher組件通過觀察者模式促進了模塊化和可擴展性。 最後,httpkernel組件簡化了請求處理和響應生成。
項目設置:
以基本的文件開頭,然後使用Composer安裝必要的組件:index.php
<code class="language-bash">php composer.phar require symfony/http-foundation symfony/http-kernel symfony/routing symfony/event-dispatcher</code>
httpfoundation:
類。 最初, httpkernel: ):
>
路由現在在>中定義:>
>類和A 。
>該框架為使用Symfony的功率和靈活性構建更複雜的應用提供了基礎。 請記住,請諮詢官方的Symfony文檔以獲取更高級的功能和組件詳細信息。 >Request
可能看起來像這樣(使用Globals):Response
index.php
通過使用HTTPFOUNDATION:<code class="language-php">switch($_SERVER['PATH_INFO']) {
case '/': echo 'Home'; break;
case '/about': echo 'About'; break;
default: echo 'Not Found!';
}</code>
<code class="language-php">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();</code>
class(例如,Core
lib/Framework/Core.php
<code class="language-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);
}
}
}</code>
index.php
>使用路由組件的路由系統增強了<code class="language-php">require 'lib/Framework/Core.php';
$request = Request::createFromGlobals();
$app = new Framework\Core();
$response = $app->handle($request);
$response->send();</code>
Core
> eventdispatcher:<code class="language-php"><?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]));
}
}</code>
eventDisPatcher組件添加了事件處理功能。 在index.php
>類中添加<code class="language-php">$app->map('/', function() { return new Response('Home'); });
$app->map('/about', function() { return new Response('About'); });
// ...</code>
方法。 (為簡潔而省略了實現詳細信息,但類似於原始輸入中的示例)。 可以使用
以上是使用Symfony組件構建自己的PHP框架的詳細內容。更多資訊請關注PHP中文網其他相關文章!