laravel入门知识点整理

吾爱主题 阅读:112 2021-10-26 15:07:00 评论:0

laravel入门

简介

作为php最常用的框架之一,laravel的框架目录布置得尤其清晰,适用于各种类型的项目开发。今天来记录下laravel入门需要熟悉的知识点。

1、根目录

其中,public/index.php是项目的入口文件

2、配置

1)config目录

该文件夹下面,包含的是各种配置文件。包括mysql数据库连接信息,redis,自定义的配置文件信息等等

2).env文件

用以存储一些依赖环境的变量,比如数据库配置,因为它不会被加入到版本库中, 所以还用以配置一些敏感信息:比如正式环境的一些第三方应用账号,token 等。有点类似yii框架中的main-local.php

用法参考:env('db_host','192.168.1.223')

说明:优先使用.env文件中配置的db_host对应的值,如果.env中没有配置,则使用这里设置的默认值'192.168.1.223'

3)用法参考

config('redis_keys.redis_keys.all_follow_user')

3、mvc

4、路由

1、routes目录

routes目录包含了应用定义的所有路由。laravel 默认提供了四个路由文件用于给不同的入口使用:web.php、api.php、 console.php 和 channels.php。 除此之外,我们还可以自定义路由文件。

这里介绍两个比较重要的官方提供的默认路由文件web.php和api.php

1)web.php

文件包含的路由通过 routeserviceprovider 引入,都被约束在 web 中间件组中,因而支持 session、csrf 保护以及 cookie 加密功能,如果应用无需提供无状态的、restful 风格的 api,那么路由基本上都要定义在 web.php 文件中

2)api.php

文件包含的路由通过 routeserviceprovider 引入,都被约束在 api 中间件组中,因而支持频率限制功能,这些路由是无状态的,所以请求通过这些路由进入应用需要通过 token 进行认证并且不能访问 session 状态。

2、路由定义

稍微复杂一点的情况:

3、routeserviceprovider

文件包含的路由通过 routeserviceprovider 引入

5、中间件

提到中间件,那一定离不开app/http/kernel.php这个文件

1) kernel

kernel 中定义了重要的中间件列表,所有的请求 request 在被应用处理前,都必须经过这些中间件,筛过一遍后,才会被决定如何处理。这涉及到中间件(middleware)的作用。

app\http\kernel

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 <?php   namespace app\http;   use illuminate\foundation\http\kernel as httpkernel;   class kernel extends httpkernel {   /**    * the application's global http middleware stack.    *    * these middleware are run during every request to your application.    *    * @var array    */   protected $middleware = [    \app\http\middleware\checkformaintenancemode:: class ,    \illuminate\foundation\http\middleware\validatepostsize:: class ,    \app\http\middleware\trimstrings:: class ,    \illuminate\foundation\http\middleware\convertemptystringstonull:: class ,    \app\http\middleware\trustproxies:: class ,    \app\http\middleware\enablecross:: class ,   ];     /**    * the application's route middleware groups.    *    * @var array    */   protected $middlewaregroups = [    'web' => [     \app\http\middleware\encryptcookies:: class ,     \illuminate\cookie\middleware\addqueuedcookiestoresponse:: class ,     \illuminate\session\middleware\startsession:: class ,     // \illuminate\session\middleware\authenticatesession::class,     \illuminate\view\middleware\shareerrorsfromsession:: class ,     \app\http\middleware\verifycsrftoken:: class ,     \illuminate\routing\middleware\substitutebindings:: class ,    ],    'api' => [ //   'throttle:300,1',     'bindings' ,    ],    'web_api' => [ //   'throttle:300,1',     'bindings' ,     'check_token'    ],    'admin_api' => [ //   'throttle:300,1',     'bindings' ,     'admin'    ],   ];     /**    * the application's route middleware.    *    * these middleware may be assigned to groups or used individually.    *    * @var array    */   protected $routemiddleware = [    'auth' => \illuminate\auth\middleware\authenticate:: class ,    'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth:: class ,    'bindings' => \illuminate\routing\middleware\substitutebindings:: class ,    'cache.headers' => \illuminate\http\middleware\setcacheheaders:: class ,    'can' => \illuminate\auth\middleware\authorize:: class ,    'guest' => \app\http\middleware\redirectifauthenticated:: class ,    'signed' => \illuminate\routing\middleware\validatesignature:: class ,    'throttle' => \illuminate\routing\middleware\throttlerequests:: class ,    'check_token' => \app\http\middleware\checktoken:: class ,   ]; }
上面的 $middleware[] 是面向全局的,特别是针对 http 以及较为底层的。后面的 $middlewaregroups[] 和 $routemiddleware[] 是比较具体的实施层面的。应该是可以根据开发需要继续添加。

我们再看看app\http\kernel继承的父类illuminate\foundation\http\kernel

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <?php   namespace illuminate\foundation\http;   use exception; use throwable; use illuminate\routing\router; use illuminate\routing\pipeline; use illuminate\support\facades\facade; use illuminate\contracts\debug\exceptionhandler; use illuminate\contracts\foundation\application; use illuminate\contracts\http\kernel as kernelcontract; use symfony\component\debug\exception\fatalthrowableerror;   class kernel implements kernelcontract {   /**    * the application implementation.    *    * @var \illuminate\contracts\foundation\application    */   protected $app;     /**    * the router instance.    *    * @var \illuminate\routing\router    */   protected $router;     /**    * the bootstrap classes for the application.    * 引导类,起引导作用的类    * 这些类里面基本上都有一个 bootstrap(application $app) 方法,    * 从不同的角度 bootstrap 应用。为最终 boot() 最准备。    * 注意:这些事做不完,不能接受请求,或许连$request都无法正确生成。    * @var array    */   protected $bootstrappers = [    // 载入服务器环境变量(.env 文件?)    \illuminate\foundation\bootstrap\loadenvironmentvariables:: class ,    // 载入配置信息(config 目录?)    \illuminate\foundation\bootstrap\loadconfiguration:: class ,    // 配置如何处理异常    \illuminate\foundation\bootstrap\handleexceptions:: class ,    // 注册 facades    \illuminate\foundation\bootstrap\registerfacades:: class ,    // 注册 providers    \illuminate\foundation\bootstrap\registerproviders:: class ,    // 启动 providers    \illuminate\foundation\bootstrap\bootproviders:: class ,   ];     /**    * the application's middleware stack.    *    * @var array    */   protected $middleware = [];     /**    * the application's route middleware groups.    *    * @var array    */   protected $middlewaregroups = [];     /**    * the application's route middleware.    *    * @var array    */   protected $routemiddleware = [];
 

总之,kernel 做了两件事,第一个是定义 $bootstraps[],做好了 boot 系统的准备,第二个是定义 各种 middleware,这些都对 $request 进行加工、处理、甄选、判断,最终为可以形成正确的、有效的 $response 做准备,都完成后,进行了 index.php 中的 $kernel->handle($request),返回 $response。

总结:

1) $request ---> $kernel { service providers/middlewares/routers } ---> $response

2) kernel 是就是个大黑箱,送入请求,输出响应,我们只管往里面添加服务、中间件、路由等等。

2) middleware

系统自带的verifycsrftoken.php

自定义的中间件checktoken.php

基本上中间件的具体过滤操作都在handle方法中完成

6、日志

1) 日志的配置文件:config/logging.php

2) logging.php

3) 使用参考

log::channel('wechatlog')->info("获取第三方平台component_access_token",['data'=>$data]);

然后执行请求完毕,就可以在storage/logs这个文件夹下面看到对应的日志记录

7、服务提供者

1)自定义服务提供者

在laravel里面,服务提供者其实就是一个工厂类。它最大的作用就是用来进行服务绑定。当我们需要绑定一个或多个服务的时候,可以自定义一个服务提供者,然后把服务绑定的逻辑都放在该类的实现中。在larave里面,要自定一个服务提供者非常容易,只要继承illuminate\support\serviceprovider这个类即可

举个栗子

app/providers/appserviceprovider.php

在这个举例里面,可以看到有一个register方法,这个方法是serviceprovider里面定义的。自定义的时候,需要重写它。这个方法就是用来绑定服务的。

2)laravel初始化自定义服务提供者的源码

3)config/app.php

从上一步的源码也能看到,laravel加载自定义服务提供者的时候,实际是从config/app.php这个配置文件里面的providers配置节找到所有要注册的服务提供者的。

参考链接:https://blog.csdn.net/qqtaizi123/article/details/95949672

原文链接:https://www.cnblogs.com/hld123/p/13626173.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容