Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException没有消息Laravel 5.5

时间:2018-01-12 11:13:41

标签: php laravel laravel-5.5

这是我的头脑。我在尝试从表单登录时遇到此错误:

  

Symfony \ Component \ HttpKernel \ Exception \

     

MethodNotAllowedHttpException

     

没有消息

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('/');
    }
}

我提交表格时的以下路线工作正常。

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']);
});

中间件/ Autheticates.php

class Autheticates
{
    public function handle($request, Closure $next, $guard = 'web')
    {
        if (!Auth::guard($guard)->check())
        {
            return redirect()->route('/');
        }
        return $next($request);
    }
}

中间件/ 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>

Error Image

4 个答案:

答案 0 :(得分:4)

您定义的路线出错。其public class NumeroPrimo { public static boolean primo(int numero){ for (int i=2; i<numero/2; i++){ if(numero%i==0){ return false; } else return true; } } } 并且应更改为get

更改此

post

到这个

Route::get('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);

答案 1 :(得分:0)

您对路线的回答应该是

Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);

答案 2 :(得分:0)

仅当您忘记窗体上的附加方法或Route方法中的错误时,才会出现此问题。

因此,请确保在表单中添加了POST / GET方法。并且不要忘记做出匹配的路线。

<form method="POST">

如果您的表单方法是post。像这样进行发布。

Route::post();

希望您理解方法定义。如果您遇到问题,请在下面评论。

答案 3 :(得分:0)

路线:: post('/', 'PostController@index');

路线:: post('/posts/create', 'PostController@create');

路线:: post('/posts', 'PostController@store');

发布或获取。如果您传递的发布值大于使用post