Laravel:RouteCollection.php中的NotFoundHttpException

时间:2017-05-29 10:34:10

标签: php laravel laravel-5 laravel-4 laravel-blade

我尝试在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输出:

route:list

我使用XMAPP并在localhost:81上发布应用程序,因为端口80已经被占用。

希望你能帮帮我..

1 个答案:

答案 0 :(得分:0)

我没有看到任何可能引发这种情况的路线。唯一的问题是,尝试访问仪表板的非身份验证用户将被重定向到/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'
]);