Laravel 5.4身份验证

时间:2017-09-02 17:23:00

标签: php laravel laravel-5.3 laravel-5.4

我正在尝试创建登录表单及其控制器,但是当我尝试登录时它无法正常工作,任何人都可以帮助我,我在laravel中非常新。

这是我的表格

<form action="/login" method="post">
        {{ csrf_field() }}

        <div class="form-group has-feedback">
            <input type="email" name="email" class="form-control" placeholder="Email">
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            <input type="password" name="password" class="form-control" placeholder="Password">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <div class="col-xs-7">
                <div class="checkbox">
                    <label>
                        <input type="checkbox"> Remember Me
                    </label>
                </div>
            </div>
            <!-- /.col -->
            <div class="col-xs-5">
                <button type="submit" class="btn btn-primary btn-raised btn-block ">Sign In</button>
            </div>
            <!-- /.col -->
        </div>
    </form>

这是我的路线

Route::get('/login', 'loginController@create');
Route::post('/login', 'loginController@store');

我的loginController是

class loginController extends Controller
{
    public function __construct(){
        $this->middleware('guest', ['except' => 'destroy']);
    }

   public function create(){

       return view('pages.admin.login');
   }


    public function store(){

       if(! auth()->attempt(request(['email', 'password']))){
            return back()->withErrors([
                'message' => 'Please check your credentials'
            ]);
        }

        return redirect('/home');
    }


}

我的用户模式是

    class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fname','oname','lname', 'email', 'phone','password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

请问我在这些地方做错了,当我输入电子邮件和密码作为我的凭证时,它只是刷新并返回登录页面

5 个答案:

答案 0 :(得分:0)

auth()->attempt()需要一个数组,你将request()的返回值作为单个参数发送,可能就是这样吗?

答案 1 :(得分:0)

以下是您的尝试功能应如何:

if(Auth::attempt( ['email'=> $request['email'],'password' => $request['password'] ])

答案 2 :(得分:0)

您的功能应该与@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_puneuniversity_mechanical__engineering__se); // Load an ad into the AdMob banner view. AdView adView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .setRequestAgent("android_studio:ad_template").build(); adView.loadAd(adRequest); Btn_Download=(Button)findViewById(R.id.Download); Btn_Download.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); Uri uri= Uri.parse("https://drive.google.com/uc?export=download&id=0B6C5vNWHm0sCSVlXZ3ljTnA3Tjg"); DownloadManager.Request request=new DownloadManager.Request(uri); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); Long reference=downloadManager.enqueue(request); } } ); } 外观相同。

Request

答案 3 :(得分:0)

LoginController

之上添加这些内容
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

并修改您的store方法

public function store(Request $request){

    $email = $request->email;
    $password = $request->password;

    if (! Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication Failed...
        return back()->withErrors([
            'message' => 'Please check your credentials'
        ]);
    }

    return redirect('/home');
}

同时从password模型中的$hidden移除User

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'remember_token',
];

希望它有用。

答案 4 :(得分:0)

您可以使用php artisan make:auth和laravel将生成登录所需的一切,包括控制器,然后您可以进入您的资源并进行编辑,使其看起来像您想象的那样。

这是我的登录控制器在使用php artisan make:auth

后的样子
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}