资讯专栏INFORMATION COLUMN

laravel框架学习--中间件middleware

Pandaaa / 2602人阅读

摘要:好久没有写文章了,记录一下这段时间学习的东西吧中间件是个非常方便的东西,能将一些逻辑实现解耦,并且在中,中间件的编写也是非常的方便。对于的中间件,他的实现原理也是和这个一样的。

好久没有写文章了,记录一下这段时间学习的东西吧
laravel中间件是个非常方便的东西,能将一些逻辑实现解耦,并且在laravel中,
中间件的编写也是非常的方便。谁用谁知道。

1.装饰器模式

laravel中的中间件使用的就是装饰器模式,什么是装饰器模式,先去了解一下吧,这里大概说一下,就是这个模式主要的就是用于解决 当一个类需要动态扩展功能的时候,使用继承的方式会让子类膨胀,并且这个扩展的功能是个公用功能的情况下,不利于功能的复用以及代码的解耦。

在laravel,使用对于使用这种模式的功能,称为请求处理管道,也就是pipeline

</>复制代码

  1. //公共接口
  2. interface middleware {
  3. public static function handle(Closure $next);
  4. }
  5. //装饰器1
  6. class MiddleStepOne implements middleware{
  7. public static function handle(Closure $next) {
  8. echo "前期处理的第一步"."
    ";
  9. $next();
  10. echo "后期处理的第一步"."
    ";
  11. }
  12. }
  13. //装饰器2
  14. class MiddleStepTwo implements middleware{
  15. public static function handle(Closure $next) {
  16. echo "前期处理的第二步"."
    ";
  17. $next();
  18. echo "后期处理的第二步"."
    ";
  19. }
  20. }
  21. function goFunc() {
  22. return function ($step,$className) {
  23. return function () use ($step,$className) {
  24. return $className::handle($step);
  25. };
  26. };
  27. }
  28. $pip = array(
  29. MiddleStepOne::class,
  30. MiddleStepTwo::class,
  31. );
  32. $pip = array_reverse($pip); //反转数组,以求达到要求的顺序运行
  33. $first = function (){
  34. echo "前期处理完毕"."
    ";
  35. }; //实际要处理的函数
  36. $a = array_reduce($pip,goFunc(),$first); //遍历pip数组,并将first作为第一个参数传递进去
  37. $a(); //执行

输出

这个就是一个简单的基于装饰器模式的管道。他的本质其实就是基于闭包和递归。

通过分析这个程序,对于最终生成的$a变量,它的值大概是这样的 MiddleStepOne.handle(MiddleStepTwo.handle(first)),当执行的时候因为在handle中有个next()函数的存在,所以这是一个递归的调用。对于laravel的中间件,他的实现原理也是和这个一样的。

2.laravel中的中间件和请求处理管道

在laravel中,我们我们可以通过设置中间件来在请求执行之前做一些预先的处理。

</>复制代码

  1. 从请求入口 public/index.php开始

重要的是这段代码:即 处理请求,返回请求的响应
$response = $kernel->handle(

</>复制代码

  1. $request = IlluminateHttpRequest::capture() //创建一个请求实例

);

接着我们进入kernel中看他的具体实现 IlluminateFoundationHttpKernel.php中


关于dispatchToRouter()函数请大家自己去看,这里就不多说了。

接下来就是激动人心的PipeLine类了,

</>复制代码

  1. container = $container;
  2. }
  3. /**
  4. * Set the object being sent through the pipeline.
  5. *
  6. * @param mixed $passable
  7. * @return $this
  8. */
  9. public function send($passable)
  10. {
  11. $this->passable = $passable;
  12. return $this;
  13. }
  14. /**
  15. * Set the array of pipes.
  16. *
  17. * @param array|mixed $pipes
  18. * @return $this
  19. */
  20. public function through($pipes)
  21. {
  22. $this->pipes = is_array($pipes) ? $pipes : func_get_args();
  23. return $this;
  24. }
  25. /**
  26. * Set the method to call on the pipes.
  27. *
  28. * @param string $method
  29. * @return $this
  30. */
  31. public function via($method)
  32. {
  33. $this->method = $method;
  34. return $this;
  35. }
  36. /**
  37. * Run the pipeline with a final destination callback.
  38. *
  39. * @param Closure $destination
  40. * @return mixed
  41. */
  42. public function then(Closure $destination)
  43. {
  44. $pipeline = array_reduce(
  45. array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
  46. );
  47. return $pipeline($this->passable);
  48. }
  49. /**
  50. * Get the final piece of the Closure onion.
  51. *
  52. * @param Closure $destination
  53. * @return Closure
  54. */
  55. protected function prepareDestination(Closure $destination)
  56. {
  57. return function ($passable) use ($destination) {
  58. return $destination($passable);
  59. };
  60. }
  61. /**
  62. * Get a Closure that represents a slice of the application onion.
  63. *
  64. * @return Closure
  65. */
  66. protected function carry()
  67. {
  68. return function ($stack, $pipe) {
  69. return function ($passable) use ($stack, $pipe) {
  70. if (is_callable($pipe)) {
  71. // If the pipe is an instance of a Closure, we will just call it directly but
  72. // otherwise we"ll resolve the pipes out of the container and call it with
  73. // the appropriate method and arguments, returning the results back out.
  74. //如果pip也就中间件函数是一个闭包可调用函数,就直接返回这个闭包函数就行了
  75. //这里我还没有找到对应的使用场景,后续补充
  76. return $pipe($passable, $stack);
  77. } elseif (! is_object($pipe)) {
  78. list($name, $parameters) = $this->parsePipeString($pipe);
  79. // If the pipe is a string we will parse the string and resolve the class out
  80. // of the dependency injection container. We can then build a callable and
  81. // execute the pipe function giving in the parameters that are required.
  82. $pipe = $this->getContainer()->make($name);
  83. $parameters = array_merge([$passable, $stack], $parameters);
  84. } else {
  85. // If the pipe is already an object we"ll just make a callable and pass it to
  86. // the pipe as-is. There is no need to do any extra parsing and formatting
  87. // since the object we"re given was already a fully instantiated object.
  88. $parameters = [$passable, $stack];
  89. }
  90. return method_exists($pipe, $this->method)
  91. ? $pipe->{$this->method}(...$parameters)
  92. : $pipe(...$parameters);
  93. };
  94. };
  95. }
  96. /**
  97. * Parse full pipe string to get name and parameters.
  98. *
  99. * @param string $pipe
  100. * @return array
  101. */
  102. protected function parsePipeString($pipe)
  103. {
  104. list($name, $parameters) = array_pad(explode(":", $pipe, 2), 2, []);
  105. if (is_string($parameters)) {
  106. $parameters = explode(",", $parameters);
  107. }
  108. return [$name, $parameters];
  109. }
  110. /**
  111. * Get the container instance.
  112. *
  113. * @return IlluminateContractsContainerContainer
  114. * @throws RuntimeException
  115. */
  116. protected function getContainer()
  117. {
  118. if (! $this->container) {
  119. throw new RuntimeException("A container instance has not been passed to the Pipeline.");
  120. }
  121. return $this->container;
  122. }
  123. }

总的来说pipeLine类的实现和我之前写的修饰器是差不多,这里主要麻烦的地方就在于就在于
protected function carry()函数内部,对于当pip是闭包,字符串,还有对象的处理。

之前觉得laravel的中间件是个很神秘的东西,但是看了之后才觉得也就那样,很精巧,在实际开发中这种模式也是很有帮助的,例如我们目前用的一个gateway项目,因为没有使用任何框架,所以将判断条件剥离,写入到中间件中, 这样实现了一定程度上的模块化编程。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29482.html

相关文章

  • Laravel学习笔记六-权限管理与间件Middleware

    摘要:而日志中间件则可以记录所有传入应用程序的请求。框架已经内置了一些中间件,包括维护身份验证保护,等等。所有的中间件都放在目录内。在中可以使用授权策略来对用户的操作权限进行验证,在用户未经授权进行操作时将返回异常。 这一节我们将给相关的动作页面添加权限,如已经登录的用户将不会看到注册、登录按钮,更不会对别人的个人资料进行编辑操作,除非是管理员,这里我们将借助Laravel提供的中间件Mid...

    RobinTang 评论0 收藏0
  • laravel框架应用和composer扩展包开发

    摘要:官方地址是目前最流行的框架,发展势头迅猛,应用非常广泛,有丰富的扩展包可以应付你能想到的各种应用场景,框架思想前卫,跟随时代潮流,提倡优雅代码,自称为工匠,其中的模板引擎容器以及扩展包为业务的开发提供了极大的便利。 laravel5.5+ laravel官方地址 laravel是目前最流行的php框架,发展势头迅猛,应用非常广泛,有丰富的扩展包可以应付你能想到的各种应用场景,lara...

    shevy 评论0 收藏0
  • lumen5.5学习(三)

    摘要:接着上篇分割线是的实例,但是文件中找不到方法在类内部看到,打开找到了方法,方法注释写的是主要用于运行应用以及发送响应主要看方法 接着上篇$app->run();--------------------分割线------------------------ $app是Application的实例,但是Application.php文件中找不到run方法在类内部看到use Concerns...

    svtter 评论0 收藏0
  • Laravel学习笔记之Middleware源码解析

    摘要:学习笔记之已经聊过使用了来设计,看源码发现其巧妙用了和的一些数组函数来设计。开发环境内置函数和看源码之前,先看下这几个内置函数的使用。学习笔记之实例化源码解析已经聊过的实例化,得到中的变量,即的实例化对象。后面再学习下的源码,到时见。 说明:本文主要学习Laravel的Middleware的源码设计思想,并将学习心得分享出来,希望对别人有所帮助。Laravel学习笔记之Decorato...

    _Dreams 评论0 收藏0
  • Laravel学习笔记之Route,Middleware和Controller参数传递

    摘要:本文主要学习总结下间参数传递。开发时经常碰到类似场景有时需要在中读取中设置的和,有时也需要在中读取中设置的参数。总结下这几个知识点,便于查阅。 本文主要学习总结下Route,Middleware,Controller间参数传递。开发时经常碰到类似场景:有时需要在Middleware中读取Route中设置的middleware parameter和route parameter,有时也需...

    zhangyucha0 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<