簡體   English   中英

Laravel:RouteCollection.php中的NotFoundHttpException

[英]Laravel: NotFoundHttpException in RouteCollection.php

我試圖在Laravel中設置路由保護並收到以下錯誤消息:

NotFoundHttpException in RouteCollection.php line 179:

in RouteCollection.php line 179
at RouteCollection->match(object(Request)) in Router.php line 533
at Router->findRoute(object(Request)) in Router.php line 512
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53

我做錯了什么或錯誤來自哪里? 昨天一切都運作良好..

我的web.php

Route::get('/', function () {
return view('welcome');
})->name('home');

Route::post('/signup', [
  'uses' => 'UserController@postSignUp', 
  'as' => 'signup' 
]);

Route::post('/signin', [ 
  'uses' => 'UserController@postSignIn', 
  'as' => 'signin' 
]);

Route::get('/dashboard', [
  'uses' => 'UserController@getDashboard',
  'as' => 'dashboard',
  'middleware' => 'auth'  
]);

我的UserController.php

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller{


public function getDashboard(){
  return view('dashboard');
}

public function postSignUp(Request $request){

  $this->validate($request, [
    'email' => 'required|email|unique:users',
    'name' => 'required|max:120',
    'password' => 'required|min:4'
  ]);

  $email = $request['email'];
  $name = $request['name'];
  $password = bcrypt($request['password']); 

  // Create a user
  $user = new User();

  $user->email = $email;
  $user->name = $name;
  $user->password = $password;

  $user->save();

  Auth::login($user); // Pass the created user for auto login

  return redirect()->route('dashboard');

}

public function postSignIn(Request $request){

  $this->validate($request, [
    'email' => 'required|email',
    'password' => 'required'
  ]); 

  if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
      return redirect()->route('dashboard');
  }
  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;
}

我的RedirectIfAuthenticated.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect()->route('home');
        }

        return $next($request);
    }
}

我的welcome.blade.php

@extends('layouts.master')

@section('title')
  Welcome!
@endsection

@section('content')
  @if(count($errors) > 0)
  <div class="row">
    <div class="col-sm-4 col-md-offset-4">
      <ul>
          @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
          @endforeach
      </ul>
    </div>
  </div>
  @endif
  <div class="row">
    <div class="col-sm-6">
      <h3>Sign Up</h3>
      <form class="" action="{{ route('signup') }}" method="post">
        <div class="form-group  {{ $errors->has('name') ? 'has-error' : '' }}">
          <label for="name">Your Name</label>
          <input class="form-control" type="text" name="name" id="name" value="{{ Request::old('name') }}">
        </div>
        <div class="form-group  {{ $errors->has('email') ? 'has-error' : '' }}">
          <label for="email">Your E-Mail</label>
          <input class="form-control" type="text" name="email" id="email" value="{{ Request::old('email') }}">
        </div>
        <div class="form-group  {{ $errors->has('password') ? 'has-error' : '' }}">
          <label for="password">Your Password</label>
          <input class="form-control" type="password" name="password" id="password" value="{{ Request::old('password') }}">
        </div>
        <button type="submit" class="btn btn-primary" name="button">Submit</button>
        <input type="hidden" name="_token" value="{{ Session::token() }}"> 
      </form>
    </div>
    <div class="col-sm-6">
      <h3>Sign In</h3>
      <form class="" action="{{ route('signin') }}" method="post">
        <div class="form-group  {{ $errors->has('email') ? 'has-error' : '' }}">
          <label for="email">Your E-Mail</label>
          <input class="form-control" type="text" name="email" id="email" value="{{ Request::old('email') }}">
        </div>
        <div class="form-group  {{ $errors->has('password') ? 'has-error' : '' }}">
          <label for="password">Your Password</label>
          <input class="form-control" type="password" name="password" id="password" value="{{ Request::old('password') }}">
        </div>
        <button type="submit" class="btn btn-primary" name="button">Submit</button>
        <input type="hidden" name="_token" value="{{ Session::token() }}"> 
      </form>
    </div>
  </div>
@endsection

php artisan route:list輸出:

路線:列表

我使用XMAPP並在localhost:81上發布應用程序,因為已經使用了端口80。

希望你能幫我..

我沒有看到任何可能觸發此問題的路線。 唯一的問題是,嘗試訪問儀表板的非身份驗證用戶將被重定向到/login ,這可能會導致此觸發器。 進行以下更改。 您還缺少訪客中間件來重定向已登錄的用戶。

重定向未經身份驗證的用戶嘗試訪問身份驗證頁面。

app/Exceptions/Handler.php

return redirect()->guest(route('home'));

您需要重定向嘗試訪問僅限訪客頁面的身份驗證用戶,因此將其重定向到儀表板而不是主頁。

app/Http/Middleware/RedirectIfAuthenticated.php

return redirect()->route('dashboard');

將訪客中間件應用於非身份驗證路由。

<?php

Route::group(['middleware' => ['guest']], function () {

    Route::get('/', function () {
        return view('welcome');
    })->name('home');

    Route::post('signup', [
        'uses' => 'UserController@postSignUp',
        'as' => 'signup'
    ]);

    Route::post('signin', [
        'uses' => 'UserController@postSignIn',
        'as' => 'signin'
    ]);
});


Route::get('dashboard', [
    'uses' => 'UserController@getDashboard',
    'as' => 'dashboard',
    'middleware' => 'auth'
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM