Home  >  Article  >  Backend Development  >  laravel5 自带的登录验证应该如何POST

laravel5 自带的登录验证应该如何POST

WBOY
WBOYOriginal
2016-06-06 20:37:091035browse

laravel5 本身就带有登录验证,比如打开 localhost/home 会自动跳转到 localhost/auth/login 如果我想在客户端中POST 一个请求到 localhost/auth/login 应该如何发呢,POST的body里要不要是不是要写上很多东西,包括email,password,_token呢 另外,登录成功或者失败了能否返回一个自定义字符串提示成功或失败了,现在自带的都是redirect的。 新手求指点

回复内容:

laravel5 本身就带有登录验证,比如打开 localhost/home 会自动跳转到 localhost/auth/login 如果我想在客户端中POST 一个请求到 localhost/auth/login 应该如何发呢,POST的body里要不要是不是要写上很多东西,包括email,password,_token呢 另外,登录成功或者失败了能否返回一个自定义字符串提示成功或失败了,现在自带的都是redirect的。 新手求指点

可以在HomeController重新定义postLogin方法
laravel5 默认的postLogin只是最基本的例子,如果能用上,那是最好了,基本上只能用作参考,正式开发中肯定有很多例外

默认的postLogin方法在
AuthenticatesAndRegistersUsers这个trait中实现
包括登陆,注册,登出等

  1. Laravel 对于表单的实现是 _token(csrf_token) + _method (post|put|patch|head|get|) + body

  2. Laravel 所有 view 都可以直接访问一个$errors(实例) 可以通过 $error->has() 判断有没有错误 $errors->all() 提取所有错误内容 错误的实现机制是 flash session 所以无论是 return redirect 还是 return view 都可以访问到 $erorrs

  3. 建议研读 https://laracasts.com/series/laravel-5-fundamentals 你就通透了

<code>@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif</code>

你可以把这个复制到模板里面观察一下

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn