如何在Laravel中定义表单内部的动作?

时间:2019-06-18 11:31:09

标签: laravel laravel-5 laravel-form

我只是想向表单添加操作,我尝试如下:

{{ Form::open(['action'=> ['AuthController@login'], 'method'=>"POST",'class'=>'login-form']) }}

但是我遇到以下错误:

Action App\Http\Controllers\AuthController@login not defined. (View: D:\server\htdocs\PMS\resources\views\custom_auth\login.blade.php)

我配置laravel集体HTML。我的代码有什么问题?

更新

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Auth;

class AuthController extends Controller
{
    function show(){
        return view('custom_auth.login');
    }

    public function login(Request $request){
        print_r($request); exit;
        $this->validate($request,[
            'email' => 'required|email',
            'password' => 'required|alphaNum|min:3'
        ]);

        $user_data = array(
            'email' => $request->get('email'),
            'password' => $request->get('password')
        );
        if(Auth::attempt($user_data)){
            return redirect('/dashboard');
        }else{
            return back()->with('error','Wrong Credential');
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用Form帮助程序时,您无需将操作放入数组中,因此请尝试:

{{ Form::open(['action'=> 'AuthController@login', 'method'=>"POST",'class'=>'login-form']) }}

当然,请确保public中存在login() AuthController方法

此外,不要忘记将其添加到您的路由文件routes/web.php

Route::post('login', 'AuthController@login');

我希望对您有帮助