Laravel登录系统报错:AuthController@login动作未定义
解决Laravel中"Action AuthController@login not defined"错误
问题重现
使用JWT和Laravel视图构建登录页面,提交凭证后返回错误:InvalidArgumentException Action AuthController@login not defined。相关配置如下:
web.php路由
Route::get ('/', [AuthController::class, 'index']); Route::post('/login', [AuthController::class, 'login'])->name('login'); Route::get ('/panel', [AuthController::class, 'getPanel']);
Login方法代码
public function login(Request $request) { $credentials = $request->only('email_aziendale', 'password'); try { if (!$token = Auth::attempt($credentials)) { return back()->with('error', 'credenziali errate'); } } catch (JWTException $e) { return back()->with('error', $e->getMessage()); } return redirect('/panel'); } public function getPanel() { return view('panel'); }
Blade视图代码
<!DOCTYPE html> <html lang="it"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="{{ asset('/css/logstyle.css') }}" /> <title>Login</title> </head> <body> <div class="main"> <div class="content"> <div class="controls"> <div class="icon"> <img src="/imagines/siconsLogo.png" alt="logos"> </div> @if(count($errors) > 0) @foreach($errors->all() as $error) <li> {{ $error }} </li> @endforeach @endif <form method="POST" action="{{ action('AuthController@login') }}" > @csrf <div class="inputs"> <svg class="login" xmlns="http://www.w3.org/2000/svg" width="44" height="40" viewBox="0 0 44 40"> <g stroke="#fff" fill="none" stroke-width="3.538" transform="translate(0 -1012.362)"> <ellipse ry="8.09" rx="8.244" cy="1022.221" cx="21.555" stroke-linecap="round" /> <path d="M1.858 1046.4c-.79 4.74 3.805 4.11 3.805 4.11H37.88s4.846.936 4.312-3.854c-.533-4.79-6.076-10.937-20.04-11.043-13.964-.106-19.504 6.047-20.294 10.786z" /> </g> </svg> <input class="input" type="email" placeholder="user@sincos.it"> <svg class="lock" xmlns="http://www.w3.org/2000/svg" width="44" height="46" viewBox="0 0 44 46"> <g transform="translate(-28.15 -974.678)" stroke="#fff" fill="none" stroke-width="3.509"> <rect ry="3.136" y="995.18" x="29.903" height="23.743" width="40.491" stroke-linecap="round" /> <path d="M49.386 1004.406v4.788" stroke-linecap="round" /> <path d="M37.073 994.83s-1.39-18.398 12.97-18.398c14.36 0 12.207 18.397 12.207 18.397" /> </g> </svg> <input class="input" type="password" placeholder="password"> </div> <div class="button-container"> <button class="button" type="submit" onclick="{{ route('/login') }}"> <div class="circle animate"></div><span class="sign-in">Sign in</span> <div class="loader"></div> </button> </div> </form> </div> </div> </div> </div> </body> </html>
解决步骤
1. 修正表单Action的调用方式
Laravel 8+版本不再支持action('Controller@method')这种简洁写法(除非手动配置命名空间),而你已经给路由命名为login,直接使用路由名称更可靠:
<form method="POST" action="{{ route('login') }}" >
2. 移除按钮错误的onclick事件
按钮的onclick="{{ route('/login') }}"是错误用法:route()函数生成的是URL字符串,不是可执行的JS代码,而且表单本身已经通过method和action定义了提交行为,无需额外绑定点击事件。修改后:
<button class="button" type="submit">
3. 给输入框添加name属性
你的login方法通过$request->only('email_aziendale', 'password')获取参数,但视图中的输入框缺少对应的name属性,导致后端无法接收到提交的数据。添加后:
<input class="input" type="email" name="email_aziendale" placeholder="user@sincos.it"> <input class="input" type="password" name="password" placeholder="password">
4. 确认控制器命名空间已引入
确保web.php顶部已经正确引入AuthController的命名空间:
use App\Http\Controllers\AuthController;
5. 清除路由缓存(可选)
如果之前执行过路由缓存命令,需要清除缓存以确保新配置生效:
php artisan route:clear
内容的提问来源于stack exchange,提问作者Leonardo Gori
相关产品推荐
相关产品推荐

