问题
This is doing my head in. I'm getting this error when trying to login from a form:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException
No message
LoginController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/dashboard';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt(['username' => $request->username, 'password' => $request->password, 'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
The following route when i submit a form is working fine.
Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);
Route::group(['middleware' => ['autheticates', 'roles']], function (){
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);
});
Middleware/Autheticates.php
class Autheticates
{
public function handle($request, Closure $next, $guard = 'web')
{
if (!Auth::guard($guard)->check())
{
return redirect()->route('/');
}
return $next($request);
}
}
Middleware/Roles.php
class Roles
{
public function handle($request, Closure $next)
{
$roles = $this->getRequiredRoleForRoute($request->route());
if ($request->user()->hasRole($roles) || $roles){
return $next($request);
}
return redirect()->route('noPermissions');
}
private function getRequiredRoleForRoute($route)
{
$actions = $route->getAction();
return isset($actions['roles']) ? $actions['roles'] : null;
}
}
login.blade.php
<form class="login-form" action="{{ route('login') }}" method="post">
{{ csrf_field() }}
<div class="login-wrap">
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<input type="text" name="username" class="form-control" placeholder="Username" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
<span class="pull-right"> <a href="#"> Forgot Password?</a></span>
</label>
<button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
<button class="btn btn-info btn-lg btn-block" type="reset">Signup</button>
</div>
</form>
回答1:
Error in the route you've defined. Its get
and should change to post
change this
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);
to this
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);
action="{{ route('login') }}" # form Submit action
回答2:
Route::post('/', 'PostController@index');
Route::post('/posts/create', 'PostController@create');
Route::post('/posts', 'PostController@store');
Post or get..if u pass post value than use post
.
回答3:
your answer of the route should be
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);
回答4:
This problem appears only if you forget attached method on your form or error in Route method.
So be sure you added method POST/GET in your form. And don't forget make matching route.
<form method="POST">
If your form method is post. make post route like this.
Route::post();
I hope you understand the method define. If you're facing problem, comment below.
来源:https://stackoverflow.com/questions/48225095/symfony-component-httpkernel-exception-methodnotallowedhttpexception-no