laravel入门

laravel入门

简介

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

1、根目录

 

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

2、配置

1)config目录

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

使用举例: config('redis_keys.redis_keys.all_follow_user')   

说明:红色标识的那个是指使用的该配置文件名

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 <?php
 2 
 3 namespace App\Http;
 4 
 5 use Illuminate\Foundation\Http\Kernel as HttpKernel;
 6 
 7 class Kernel extends HttpKernel
 8 {
 9     /**
10      * The application's global HTTP middleware stack.
11      *
12      * These middleware are run during every request to your application.
13      *
14      * @var array
15      */
16     protected $middleware = [
17         \App\Http\Middleware\CheckForMaintenanceMode::class,
18         \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
19         \App\Http\Middleware\TrimStrings::class,
20         \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
21         \App\Http\Middleware\TrustProxies::class,
22         \App\Http\Middleware\EnableCross::class,
23     ];
24 
25     /**
26      * The application's route middleware groups.
27      *
28      * @var array
29      */
30     protected $middlewareGroups = [
31         'web' => [
32             \App\Http\Middleware\EncryptCookies::class,
33             \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
34             \Illuminate\Session\Middleware\StartSession::class,
35             // \Illuminate\Session\Middleware\AuthenticateSession::class,
36             \Illuminate\View\Middleware\ShareErrorsFromSession::class,
37             \App\Http\Middleware\VerifyCsrfToken::class,
38             \Illuminate\Routing\Middleware\SubstituteBindings::class,
39         ],
40         'api' => [
41 //            'throttle:300,1',
42             'bindings',
43         ],
44         'web_api' => [
45 //            'throttle:300,1',
46             'bindings',
47             'check_token'
48         ],
49         'admin_api' => [
50 //            'throttle:300,1',
51             'bindings',
52             'admin'
53         ],
54     ];
55 
56     /**
57      * The application's route middleware.
58      *
59      * These middleware may be assigned to groups or used individually.
60      *
61      * @var array
62      */
63     protected $routeMiddleware = [
64         'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
65         'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
66         'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
67         'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
68         'can' => \Illuminate\Auth\Middleware\Authorize::class,
69         'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
70         'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
71         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
72         'check_token' => \App\Http\Middleware\CheckToken::class,
73     ];
74 }
View Code

上面的 $middleware[] 是面向全局的,特别是针对 HTTP 以及较为底层的。后面的 $middlewareGroups[] 和 $routeMiddleware[] 是比较具体的实施层面的。应该是可以根据开发需要继续添加。

我们再看看App\Http\Kernel继承的父类Illuminate\Foundation\Http\Kernel

 1 <?php
 2 
 3 namespace Illuminate\Foundation\Http;
 4 
 5 use Exception;
 6 use Throwable;
 7 use Illuminate\Routing\Router;
 8 use Illuminate\Routing\Pipeline;
 9 use Illuminate\Support\Facades\Facade;
10 use Illuminate\Contracts\Debug\ExceptionHandler;
11 use Illuminate\Contracts\Foundation\Application;
12 use Illuminate\Contracts\Http\Kernel as KernelContract;
13 use Symfony\Component\Debug\Exception\FatalThrowableError;
14 
15 class Kernel implements KernelContract
16 {
17     /**
18      * The application implementation.
19      *
20      * @var \Illuminate\Contracts\Foundation\Application
21      */
22     protected $app;
23 
24     /**
25      * The router instance.
26      *
27      * @var \Illuminate\Routing\Router
28      */
29     protected $router;
30 
31     /**
32      * The bootstrap classes for the application.
33      * 引导类,起引导作用的类
34      * 这些类里面基本上都有一个 bootstrap(Application $app) 方法,
35      * 从不同的角度 bootstrap 应用。为最终 boot() 最准备。
36      * 注意:这些事做不完,不能接受请求,或许连$request都无法正确生成。
37      * @var array
38      */
39     protected $bootstrappers = [
40         // 载入服务器环境变量(.env 文件?)
41         \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
42         // 载入配置信息(config 目录?)
43         \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
44         // 配置如何处理异常
45         \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
46         // 注册 Facades
47         \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
48         // 注册 Providers
49         \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
50         // 启动 Providers
51         \Illuminate\Foundation\Bootstrap\BootProviders::class,
52     ];
53 
54     /**
55      * The application's middleware stack.
56      *
57      * @var array
58      */
59     protected $middleware = [];
60 
61     /**
62      * The application's route middleware groups.
63      *
64      * @var array
65      */
66     protected $middlewareGroups = [];
67 
68     /**
69      * The application's route middleware.
70      *
71      * @var array
72      */
73     protected $routeMiddleware = [];
View Code

总之,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

posted @ 2020-09-07 12:36  欢乐豆123  阅读(602)  评论(0编辑  收藏  举报