问题描述
我正在查看内置的身份验证控制器,发现它们使用了称为"Guards"的东西.直到现在,每当我自己创建登录名/注册表时,我都从未碰过这些,通常只会做以下事情:
I was looking through the built in auth controllers and I noticed they use something called "Guards". Up until now whenever I made my own logins/register forms I never touched these and would usually just do things like:
Auth::attempt()
没有任何类型的防护.我曾尝试查找确切的内容,但我真的找不到任何信息,有人可以向我解释一下警卫的目的是什么?
without any type of guard. I've tried looking up what exactly it is but I couldn't really find any information on it, could someone explain to me what the purpose of the guards are?
推荐答案
它们是系统应如何存储和检索有关用户信息的定义.
They're the definition of how the system should store and retrieve information about your users.
您可以在config/auth.php
文件中找到配置. Web Guard是传统的Cookie存储-因此Web Guard指示Laravel以经典方式存储和检索会话信息.另一方面,API保护使用令牌.因此,如果您想使用标头(载体)或查询参数中的API令牌对用户和请求进行身份验证,则可以使用API保护.
You can find the configuration in your config/auth.php
file. A web guard is the traditional cookie store - so that web guard instructs Laravel to store and retrieve session information the classic way. The API guard, on the other hand, uses tokens. So you would use the API guard if you want to authenticate users and requests using an API token in the header (bearer) or query parameter.
如果愿意,您也可以创建自己的后卫,并且还有一个很好的介绍博客文章关于Matt Stauffer的话题.
You can also create your own guard if you wish, and there's also this good introductory blog post on the topic by Matt Stauffer.
这篇关于Laravel什么是警卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!