我对Laravel很新,我面临一个模糊的错误。每当我尝试使用用户名和密码登录时,都会收到此错误。
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
我的代码由以下内容组成:
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
class UserController extends Controller
{
public function postSignUp(Request $request){
$firstName = $request['firstName'];
$lastName = $request['lastName'];
$username = $request['username'];
$password = bcrypt($request['password']);
$email = $request['email'];
$user = new User();
$user->first_name = $firstName;
$user->last_name = $lastName;
$user->username = $username;
$user->password = $password;
$user->email = $email;
$user->save();
Auth::login($user);
return redirect()->back();
}
public function postSignIn(Request $request){
$username = $request['username'];
$password = $request['password'];
if (Auth::attempt(['username' => $username, 'password' => $password])){
return redirect()->back();
}
}
}
提供商名为User.php
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
}
路由文件web.php
<?php
Route::get('/', 'PagesController@index')->name('home');
Route::post('signup', 'UserController@postSignUp')->name('signup');
Route::get('signin', 'UserController@postSignin')->name('signin');
答案 0 :(得分:4)
MethodNotAllowed意味着您正在使用网络服务器对该请求不喜欢的VERB ...即GET而不是POST。
您的登录控制台名为postSignIn
,但我注意到您使用get
答案 1 :(得分:0)
这是因为路线不存在。在路线后添加标志。 postSignin应该是一个后期路线
Route::post('signin', 'UserController@postSignin')->name('signin');
Route::get('signin', 'UserController@getSignin')->name('signInForm');
而不是
Route::get('signin', 'UserController@postSignin')->name('signin’);