返回
热门搜索

laravel入门知识点整理

IT博客 结构与算法 点击量 300

基本信息 收藏 - 举报 - 海报

详细介绍

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)的作用。

AppHttpKernel

<?php

namespace AppHttp;

use IlluminateFoundationHttpKernel 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 = [
  AppHttpMiddlewareCheckForMaintenanceMode::class,
  IlluminateFoundationHttpMiddlewareValidatePostSize::class,
  AppHttpMiddlewareTrimStrings::class,
  IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
  AppHttpMiddlewareTrustProxies::class,
  AppHttpMiddlewareEnableCross::class,
 ];

 /**
  * The application"s route middleware groups.
  *
  * @var array
  */
 protected $middlewareGroups = [
  "web" => [
   AppHttpMiddlewareEncryptCookies::class,
   IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
   IlluminateSessionMiddlewareStartSession::class,
   // IlluminateSessionMiddlewareAuthenticateSession::class,
   IlluminateViewMiddlewareShareErrorsFromSession::class,
   AppHttpMiddlewareVerifyCsrfToken::class,
   IlluminateRoutingMiddlewareSubstituteBindings::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" => IlluminateAuthMiddlewareAuthenticate::class,
  "auth.basic" => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
  "bindings" => IlluminateRoutingMiddlewareSubstituteBindings::class,
  "cache.headers" => IlluminateHttpMiddlewareSetCacheHeaders::class,
  "can" => IlluminateAuthMiddlewareAuthorize::class,
  "guest" => AppHttpMiddlewareRedirectIfAuthenticated::class,
  "signed" => IlluminateRoutingMiddlewareValidateSignature::class,
  "throttle" => IlluminateRoutingMiddlewareThrottleRequests::class,
  "check_token" => AppHttpMiddlewareCheckToken::class,
 ];
}
上面的 $middleware[] 是面向全局的,特别是针对 HTTP 以及较为底层的。后面的 $middlewareGroups[] 和 $routeMiddleware[] 是比较具体的实施层面的。应该是可以根据开发需要继续添加。

我们再看看AppHttpKernel继承的父类IlluminateFoundationHttpKernel

<?php

namespace IlluminateFoundationHttp;

use Exception;
use Throwable;
use IlluminateRoutingRouter;
use IlluminateRoutingPipeline;
use IlluminateSupportFacadesFacade;
use IlluminateContractsDebugExceptionHandler;
use IlluminateContractsFoundationApplication;
use IlluminateContractsHttpKernel as KernelContract;
use SymfonyComponentDebugExceptionFatalThrowableError;

class Kernel implements KernelContract
{
 /**
  * The application implementation.
  *
  * @var IlluminateContractsFoundationApplication
  */
 protected $app;

 /**
  * The router instance.
  *
  * @var IlluminateRoutingRouter
  */
 protected $router;

 /**
  * The bootstrap classes for the application.
  * 引导类,起引导作用的类
  * 这些类里面基本上都有一个 bootstrap(Application $app) 方法,
  * 从不同的角度 bootstrap 应用。为最终 boot() 最准备。
  * 注意:这些事做不完,不能接受请求,或许连$request都无法正确生成。
  * @var array
  */
 protected $bootstrappers = [
  // 载入服务器环境变量(.env 文件?)
  IlluminateFoundationBootstrapLoadEnvironmentVariables::class,
  // 载入配置信息(config 目录?)
  IlluminateFoundationBootstrapLoadConfiguration::class,
  // 配置如何处理异常
  IlluminateFoundationBootstrapHandleExceptions::class,
  // 注册 Facades
  IlluminateFoundationBootstrapRegisterFacades::class,
  // 注册 Providers
  IlluminateFoundationBootstrapRegisterProviders::class,
  // 启动 Providers
  IlluminateFoundationBootstrapBootProviders::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里面,要自定一个服务提供者非常容易,只要继承IlluminateSupportServiceProvider这个类即可

举个栗子

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

没有更多内容。

用户评价(0)

好评度100%
  • 还没有人评论此条信息!
+ 加载更多

联系方式

提示:联系我时,请说明在看到的,谢谢!
  • 联系人:
  • 地  区:
  • 电  话: 共发布信息(2004)条 所在地:未填写
看了又看
加载中
首页 首页 收藏 收藏

电话联系