一次框架性能的比较,引起了我对搭建web框架的兴趣 (3)

在phoenix项目下,创建routes文件夹,在routes下继续创建web.yaml文件。

dashboard: path: /dashboard defaults: { _controller: 'App\Http\Controllers\DashboardController::index' }

下载symfony的Config组件、Yaml组件、Routing组件。

composer require symfony/config composer require symfony/yaml composer require symfony/routing

Config组件使用说明

更新代码

# index.php ini_set('display_errors', 1); error_reporting(-1); require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Loader\YamlFileLoader; # add use Symfony\Component\Config\FileLocator; # add $request = Request::createFromGlobals(); $fileLoader = new YamlFileLoader(new FileLocator(array(__DIR__))); # add $collection = $fileLoader->load('routes/web.yaml'); # add $name = $request->get('name', 'World'); $response = new Response(); $response->setContent('<b>Hello '.$name.'</b>'); $response->send();

dump($collection),可以看到返回了路由的Collection对象,里面有定义的路由。

5

这个时候,框架只是得到了定义的路由,但还没有和URL做映射,下面改造继续。

URL和配置路由映射

ini_set('display_errors', 1); error_reporting(-1); require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; use Symfony\Component\Routing\RequestContext; # add use Symfony\Component\Routing\Matcher\UrlMatcher; # add $request = Request::createFromGlobals(); $fileLoader = new YamlFileLoader(new FileLocator(array(__DIR__))); $collection = $fileLoader->load('routes/web.yaml'); #解析url $context = new RequestContext(); # add $context->fromRequest($request); # add #初始化UrlMatcher $matcher = new UrlMatcher($collection, $context); # add #url和路由配置映射 $route = $matcher->match($request->getPathInfo()) # add $name = $request->get('name', 'World'); $response = new Response(); $response->setContent('<b>Hello '.$name.'</b>'); $response->send();

继续分析。

$context = new RequestContext(); $context->fromRequest($request);

context对象主要就是对url进行解析。现在的域名:

6

既然解析出url的参数,就要用解析出的参数和配置中的路由做精准关联了,初始化matcher,传入路由配置和url对象。

7

得到url和配置中的路由的映射。

$route = $matcher->match($request->getPathInfo());

8

五、控制器处理相应功能(C)

在路由处理中,框架已经得到了路由和控制器的关联关系。下面就要执行相应的控制器(上面的_controller值)。

首先,在phoenix项目下,创建app/Http/Controllers/DashboardController.php(仿造Laravel的目录结构)。

# DashboardController.php namespace App\Http\Controllers; # 注意这里App命名空间,自己定义,并没有注册到autoload class DashboardController{ public function index() { echo 'Hello SexyPhoenix'; } }

App命名空间是框架定义的,需要注册后,才能用,打开项目的composer.json文件。

# composer.json "autoload": { "psr-4": { "App\\": "app/" } } composer dump-autoload # 更新命名空间

到这里,控制器的准备工作就做完了,接下来的问题就是如果利用得到的路由和控制器的映射关系去执行控制器,也就是下面的代码。

App\Http\Controllers\DashboardController::index

其实也很简单,就是用"::"分隔,得到两个值,一个是类名,一个是方法名,再用php的call_user_func去执行。

但自己去写可能过去粗暴,可用性低,在执行前,要先判断DashboardController类是否存在,index方法是否存在,index方法的权限,是否是公共方法,以及各种参数等等,

自己去写的话,会很麻烦,为了方便,继续用symfony的组件。

composer require symfony/http-kernel

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwxwyx.html