当前位置: 首页 > 后端技术 > PHP

从symfony框架到一个完整的项目需要多少步?(3)入口文件是干什么的?

时间:2023-03-29 17:54:56 PHP

前言对于PHP的框架,不管是yii、symfony还是laravel,大家都有所涉猎。对于框架中存放的资源包vendor文件夹和入口文件(index.php或者app.php),大家也是天天遇到。但是你真的熟悉这些文件/文件夹吗?一个完整的项目是如何从一个纯框架开发出来的?每个部分在构建这座建筑中扮演什么角色?3.Symfony总是说服务器端删除了缓存。具体删除了什么?3.1入口文件...$loader=require_once__DIR__.'/../app/autoload.php';require_once__DIR__.'/../app/bootstrap.php.cache';$kernel=newAppKernel('prod',false);$kernel->loadClassCache();$request=Request::createFromGlobals();$kernel->setRequest($request);$response=$kernel->handle($request);$response->send();$kernel->terminate($request,$response);...我们查看了这个文件,发现autoload文件一上来就必须引入。上一篇文章讲了很久,讲的是一个依赖文件的问题。我们必须把这个依赖。下面重点讲解AppKernel和Application。首先我们看一下AppKernel.php中的boot方法。这个方法是整个Appker类的boot方法,也是核心。它在内部做了以下工作。实例化Bundle实例化容器实例化BIZ类以引导每个Bundlepublicfunctionboot(){if(true===$this->booted){return;}if($this->loadClassCache){$this->doLoadClassCache($this->loadClassCache[0],$this->loadClassCache[1]);}/***实例化Bundle。*/$this->initializeBundles();/***根据cache/简墨/appDevDebugProjectContainer.php.meta是否存在,文件内容(序列化)是否与配置文件一致,判断是否生成cache/简墨/*appDevDebugProjectContainer.php文件及相关文件*内容appDevDebugProjectContainer.php的实现如下*初始化appDevDebugProjectContainer类(其中methodMap对象是“类和实例化类的方法”的对照表)*初始化完成后,调用$this->>getContainer->Get($methodMapClassName)可以实现该类的实例化*/$this->initializeContainer();/***调用appDevDebugProjectContainer->getBizService()方法*实例化Biz类*初始化参数由app/config/biz.yml提供*/$this->initializeBiz();/***AppKernel是一个重要的文件*无论在控制台命令行模式/浏览器请求模式,它都起着至关重要的作用*它是symfony内核的封装*该方法初始化内核*/$this->initializeServiceKernel();foreach($this->getBundles()as$bundle){$bundle->setContainer($this->container);$bundle->boot();}$this->booted=true;}3.1.2InstantiateBundle3.1.2Instantiatecontainer方法写在vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php是symfony写的一个底层方法,这里详细说说它的作用,形成一个受保护的函数initializeContainer(){$class=$这个->getContainerClass();$cache=newConfigCache($this->getCacheDir().'/'.$class.'.php',$this->debug);$新鲜=真;如果(!$cache->isFresh()){$container=$this->buildContainer();$容器->编译();$this->dumpContainer($cache,$container,$class,$this->getContainerBaseClass());$新鲜=假;}require_once$cache->getPath();/***实例化新生成的app[Dev|Prod]]DebugProjectContainer类。*我们看一下类,在__construct*/$this->container=new$class();$this->container->set('内核',$this);如果(!$fresh&&$this->container->has('cache_warmer')){$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.缓存目录'));}}如果我们此时打开app/cache/[prod|dev]/Jianmo/app[Dev|Prod]]DebugProjectContainer.php文件,会发现在__construct()中会加载类名和方法被写入一个大数组。代码如下,如果我们要初始化biz类,只需要调用getBizService方法即可。而这个方法也在当前文件中。...公共函数__construct(){...$this->methodMap=array('annotation_reader'=>'getAnnotationReaderService','app.locale_listener'=>'getApp_LocaleListenerService','biz'=>'getBizService'...);}