简体   繁体   中英

Laravel: NotFoundHttpException in RouteCollection.php

I tried to set up a route protection in Laravel and got the following error message:

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

What am I doing wrong or where does the error come from? Yesterday everything worked well..

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

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

}

My 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;
}

My 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);
    }
}

My 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 outputs this:

路线:列表

I use XMAPP and publish the application on localhost:81 because port 80 was already taken.

Hope you can help me..

I don't see any routes that might trigger this. Only thing is that non auth users trying to reach your dashboard would be redirected to /login which might cause this trigger. Make the following changes. You're also missing guest middleware to redirect users who are already logged in.

Redirect unauthenticated users trying to access auth pages.

app/Exceptions/Handler.php

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

You need to redirect auth users trying to access guest only page, so redirect them to dashboard and not home.

app/Http/Middleware/RedirectIfAuthenticated.php

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

Apply guest middleware to non-auth routes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM