laravel中间件_使用Laravel中间件中断应用程序

laravel中间件

Laravel Course Image of Rocket

Before we launch into using middleware with Laravel, what is middleware? In general, middleware is software that connects different systems to one another. In Laravel, it’s one of the most well known and robust PHP frameworks that are available for use today, middleware provides a way for you to filter out your HTTP requests and manage those requests. This allows you to dictate how those requests interact with your application.

在我们开始将中间件与Laravel结合使用之前,什么中间件? 通常,中间件是将不同系统相互连接的软件。 在Laravel中 ,它是当今可用的最著名,最健壮PHP框架之一,中间件为您提供了一种过滤HTTP请求并管理这些请求的方法。 这使您能够决定那些请求如何与您的应用程序交互。

为什么我需要Laravel的中间件? (Why Do I Need Laravel’s Middleware?)

The use of middleware in Laravel 5 can interrupt requests to your application. But that’s pretty opaque, isn’t it? How can that actually help you? If you’re already very familiar with Laravel, or the interaction of various software systems, you may already know this answer. If not, bear with us.

Laravel 5中使用中间件可能会中断对应用程序的请求。 但这很不透明,不是吗? 这实际上能为您提供帮助吗? 如果您已经非常熟悉Laravel或各种软件系统的交互,那么您可能已经知道此答案。 如果没有,请和我们一起忍受。

您需要Laravel的中间件来中断东西 (You Need Laravel’s Middleware to Interrupt Stuff)

Laravel’s middleware can interrupt those requests and then perform various actions such as controlling access and requiring authentication to enter certain areas. So if you picture your application, it’s just an app, hanging out, and accepting incoming web requests and serving up your views, or API responses, or whatever else you’re delivering. But how does your application manage those requests? How does it filter them out? How does it know whether to check for your user credentials, or whether they’re authorized to do what the request is asking to do? Or how does your application know, based on the content of a request, what to do with that request specifically?

Laravel的中间件可以中断这些请求,然后执行各种操作,例如控制访问权限和要求身份验证才能进入某些区域。 因此,如果您对自己的应用程序进行了描述,那么它只是一个应用程序,它可以挂出并接受传入的Web请求,并提供您的视图,API响应或您提供的任何其他内容。 但是您的应用程序如何管理这些请求? 如何过滤掉它们? 它如何知道是否要检查您的用户凭据,或者是否已授权他们执行请求所要求的操作? 或者您的应用程序如何根据请求的内容知道如何处理该请求?

The answer, of course, is middleware. Middleware can be used for authentication-related matters, but it can also be used for things like initiating logging, or for rate limits on your API. There are numerous use cases for middleware, and Laravel’s middleware allows you an elegant approach to handling these cases rather than trying to hack something together for each instance.

答案当然是中间件。 中间件可用于与身份验证有关的事务,但也可用于诸如启动日志记录或API速率限制之类的事情。 中间件有很多用例,Laravel的中间件使您可以用一种优雅的方式来处理这些用例,而不必试图为每个实例一起破解一些东西。

工匠– Laravel的主力军 (Artisan – Laravel’s Workhorse)

And it really is the opposite of a hack. Laravel’s middleware is incredibly easy to use, in no small part because of Artisan, the command line interface tool that comes baked into Laravel 5. Artisan provides a simple way to create middleware, which can then be edited. You add your logic, and then you must configure it to be used by whichever routes you see fit, and presto, you now have working middleware.

这确实是黑客的对立面。 Laravel的中间件是非常容易使用,因为在没有小部分工匠 ,在命令行界面工具,自带烤成Laravel 5.工匠提供了一种简单的方法来创建中间件,然后可以进行编辑。 您添加了逻辑,然后必须配置它以供您认为合适的任何路由使用,并且,到现在,您已经可以使用中间件。

观看魔术 (Watch the Magic)

TL;DR? This video will walk you through using middleware in your Laravel 5 application, and show you some practical use for it, using Laravel 5’s middleware documentation for reference. With Laravel’s middleware, you will be able to control the flow of requests to your application with ease!

TL; DR? 该视频将带您逐步了解如何在Laravel 5应用程序中使用中间件,并使用Laravel 5的中间件文档作为参考,向您展示一些实际用途。 使用Laravel的中间件,您将能够轻松控制对应用程序的请求流!

This video will also show you how to implement the middleware that you’ve constructed from your routes, so that requests to particular routes will now use the middleware that you have constructed and run that before anything further is done with the request. It’s simple, yet effective, and Laravel makes this process incredibly easy to handle.

该视频还将向您展示如何实现通过路由构造的中间件,以便对特定路由的请求现在将使用您构造的中间件并运行该中间件,然后再对请求进行进一步的处理。 它简单但有效,Laravel使此过程非常容易处理。

Loading the player…
正在加载播放器…

仍想了解有关Laravel 5的更多信息吗? (Still Hungry for More About Laravel 5?)

We have a comprehensive Build a Blog with Laravel ready for you! Head on over to SitePoint to get started! This course will provide you with a firm understanding of Laravel 5 PHP development. It will give you the instructions you need to learn to install Laravel from scratch, use Laravel tools and methods, separate the various aspects of a Laravel application, and to handle the basic aspects of Create, Read, Update, and Delete (CRUD) system with Laravel.

我们为您准备了一个与Laravel全面的构建博客 ! 前往SitePoint开始! 本课程将使您对Laravel 5 PHP开发有深入的了解。 它将为您提供从头开始安装Laravel,使用Laravel工具和方法,分离Laravel应用程序各个方面以及处理创建,读取,更新和删除(CRUD)系统的基本方面所需的说明。与Laravel。

Your instructor, Isaac Castillo, has been working in the design and web application development fields for more than fifteen years. He works extensively with PHP, Laravel, WordPress, React Native, and other technologies, and previously a LAMP stack coding boot camp instructor. Neat, huh!

您的老师Isaac Castillo在设计和Web应用程序开发领域已经工作了15年以上。 他广泛使用PHP,Laravel,WordPress,React Native和其他技术,并且以前是LAMP堆栈编码新手训练师。 整洁,呵呵!

翻译自: https://www.sitepoint.com/middleware-in-laravel-5/

laravel中间件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值