继续学习lumen5.5--------------------分割线------------------------看如何输出'Lumen(5.5.2)(LaravelComponents5.5.*)'这个数据public目录下的index.php加载app.phprequire_once__DIR__.'/../vendor/autoload下bootstrap.php';//Composer自动加载(newDotenv\Dotenv(__DIR__.'/../'))->load();//加载.env的配置$app=newLaravel\Lumen\Application(realpath(__DIR__.'/../'));//初始化应用程序//初始化内容publicfunction__construct($basePath=null){if(!empty(env('APP_TIMEZONE'))){date_default_timezone_set(env('APP_TIMEZONE','UTC'));}//指定项目基目录$this->basePath=$basePath;//注册服务容器$this->bootstrapContainer();//注册异常处理$this->registerErrorHandling();//实例化Route路由类$this->bootstrapRouter();}然后将核心组件注册到服务容器中(laravel的服务容器稍后学习)主要看$app->router->group(['namespace'=>'App\Http\Controllers',],function($router){require__DIR__.'/../routes/web.php';});加载routes文件,这样就可以全部添加到app中了,这个会提供所有请求接口的response的第一个参数是指定处理接口的属性设置,namespace属性是指定controller的目录号处理请求。第二个参数是一个闭包函数,传递一个匿名函数给group方法找到web.php$router->get('/',function()use($router){return$router->app->version();});将web.php定义的路由放在这个匿名函数中,相当于下面的$app->router->group(['namespace'=>'App\Http\Controllers',],function($router){$router->get('/',function()use($router){return$router->app->version();});});然后看Router类Method中的group,有一个call_user_func($callback,$this);这段代码执行传入的匿名函数,就是web.php定义的所有路由$router->get('/',function()use($router){return$router->app->version();});找到Router类中的get方法,看到调用了addRoute方法。看到名字/***Addaroutetothecollection大概就知道添加路由的意思了。**@paramarray|string$method*@paramstring$uri*@parammixed$action*@returnvoid*/publicfunctionaddRoute($method,$uri,$action){$action=$this->parseAction($行动);$属性=空;如果($this->hasGroupStack()){$attributes=$this->mergeWithLastGroup([]);}if(isset($attributes)&&is_array($attributes)){if(isset($attributes['prefix'])){$uri=trim($attributes['prefix'],'/').'/'.trim($uri,'/');}if(isset($attributes['suffix'])){$uri=trim($uri,'/').rtrim($attributes['suffix'],'/');$action=$this->mergeGroupAttributes($action,$attributes);}$uri='/'.trim($uri,'/');如果(isset($action['as'])){$this->namedRoutes[$action['as']]=$uri;}if(is_array($method)){foreach($methodas$verb){$this->routes[$verb.$uri]=['method'=>$verb,'uri'=>$uri,'动作'=>$动作];}}else{$this->routes[$method.$uri]=['method'=>$method,'uri'=>$uri,'action'=>$action];}}里面做的就是把在web.php中定义的路由翻译成你你想要的处理方式最终放在了$routes属性中。这里可以参考文档的[HTTP路由][1]部分(这是旧版的中文文档,新版要看官网的英文版,不同的是$app改为$route,旧的路由定义文件是routes.php,新的是web.php)可以将上面的请求路由代码翻译成,当请求路由为'api.com/index.php/'时,调用匿名函数函数()使用($router){return$router->app->version();}响应;可以看出,执行匿名函数时调用了应用类中的version方法publicfunctionversion(){return'Lumen(5.5.2jjj)(LaravelComponents5.5.*)';}PS:这只是return,不是echo输出,继续往下看,$app->run();下次我会添加它......
