前言标题改了。读者使用第1、2、3章比较麻烦,目前的标题是根据项目的进度来写的。上一篇文章的地址是https://segmentfault.com/a/11...本系列文章不打算写太多章节,只计划了大概4~5章。具体实现代码请移步Githubhttps://github.com/CrazyCodes...本章详细讲解Route(路由的实现)。上图ComeonUpImage大致可以看出,实现路由需要两步,将所有的路由信息??存储在超全局变量中。当用户请求从全局变量中找到路由映射的服务脚本并实例化就OK了。过程大概和姜子一样。“滚动”目录路由的代码暂时分为以下几个文件(这个不确定,具体可以参考Github)被处理和存储。RouteInterface不需要解释RouteModel路由模型,将每条路由信息以结构体形式存储在$_SERVERRouter路由核心类中。别着急,让我们一个一个地看文件。.从路由接口开始。RouteInterface参考RESTful规定设置接口方法为GET、POST、PATCH、PUT、DELETE和OPTIONS。当然,Laravel也对上述标准请求进行了规范。GitHub:https://github.com/CrazyCodes...interfaceRouteInterface{/***@param$uri*@paramnull$action**@returnmixed*/publicfunctionget($uri,$action=null);/***@param$uri*@paramnull$action**@returnmixed*/publicfunctionpost($uri,$action=null);/***@param$uri*@paramnull$action**@returnmixed*/publicfunctionpatch($uri,$action=null);/***@param$uri*@paramnull$action**@returnmixed*/publicfunctionput($uri,$action=null);/***@param$uri*@paramnull$action**@returnmixed*/publicfunctiondelete($uri,$action=null);/***@param$uri*@paramnull$action**@returnmixed*/publicfunctionoptions($uri,$action=null);}路由器先写一个栗子publicfunctionget($uri,$action=null){返回$this->addRoute("GET",$uri,$action);}用户调用下面的代码指向上面的方法,调用addRoute方法将路由信息存储在$_SERVERRoute::get('/','Controller')下面是addRoute部分的代码publicfunctionaddRoute($methods,$uri,$action){//这里判断请求方式是否合规,是??否有GET,POST,PATCH其中之一,PUT,DELETE,OPTIONSif($this->verify($methods)==false){返回false;}//然后我们进入RouteCollection路由信息的处理类return$this->routes->add($uri,$this->createRoute($methods,$action));}RouteCollection最后到达add方法,并且将路由信息存储在$_SERVER中publicfunctionadd($uri,RouteModel$model){if(empty($_SERVER["routes"][$uri])){$_SERVER["routes"][$uri]=$模型;}}第二个参数,RouteModel,我们一开始就说过,这是路由模型,每条路由都以结构体的形式存储在一个变量中。'routes'存储结果后=>array(6){'test/get'=>classZero\Routing\RouteModel#13(2){public$method=>string(3)"GET"public$action=>string(19)"testController@test"}'test/post'=>classZero\Routing\RouteModel#14(2){public$method=>string(4)"POST"public$action=>string(19)"testController@test"}'test/put'=>classZero\Routing\RouteModel#15(2){public$method=>string(3)"PUT"public$action=>string(18)"testController@put"}'test/del'=>classZero\Routing\RouteModel#16(2){public$method=>string(6)"DELETE"public$action=>string(18)"testController@del"}'test/patch'=>classZero\Routing\RouteModel#17(2){public$method=>string(5)"PATCH"public$action=>string(20)"testController@patch"}'test/opt'=>classZero\Routing\RouteModel#18(2){public$method=>string(7)"OPTIONS"public$action=>string(18)"testController@opt"}}Route最后通过__callStatic将代码重定向核心类中publicstaticfunction__callStatic($name,$arguments){$router=newRouter;返回$router->{$name}($arguments[0],$arguments[1]);}以上例程部分是Laravel的设计思路。通过这个简单的框架,你可以对Laravel的核心设计有一点了解。上次的测试测试有点粗糙。从本章到本系列结束,我们都在使用PHPunit进行测试。/***@content测试所有方法存储->$_SERVER["routes"]*/publicfunctiontestAllMethodsStorage(){$this->routes->get($methodGet="test/get","testController@test");$this->assertArrayHasKey($methodGet,$_SERVER[$this->methodsDataKey]);$this->routes->post($methodPost="test/post","testController@test");$this->assertArrayHasKey($methodPost,$_SERVER[$this->methodsDataKey]);$this->routes->put($methodPut="test/put","testController@put");$this->assertArrayHasKey($methodPut,$_SERVER[$this->methodsDataKey]);$this->routes->delete($methodDel="test/del","testController@del");$this->assertArrayHasKey($methodDel,$_SERVER[$this->methodsDataKey]);$this->routes->patch($methodPatch="test/patch","testController@patch");$this->assertArrayHasKey($methodPatch,$_SERVER[$this->methodsDataKey]);$this->路线->选项($methodOpt="测试/选择","testController@opt");$this->assertArrayHasKey($methodOpt,$_SERVER[$this->methodsDataKey]);}粘贴上面的部分代码,以程序的方式进行测试,看看存储是否符合预期。/***@contentRouteModel成功*/publicfunctiontestCreateRoute(){$response=$this->routes->createRoute("GET","TestController@Get");$this->assertInstanceOf(RouteModel::class,$response);}包括在创建路由后测试是否是RouteModel实现。具体可以参考Githubhttps://github.com/CrazyCodes...感谢上面路由的基本设计,下一章会讲解从启动到请求路由映射到服务脚本的过程。希望这一章能帮到你,谢谢。
