>本教程展示了使用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中文网其他相关文章!