我遇到了一个问题,我错过了路线的行为。如果我尝试随机URL,则不会抛出任何异常,我会得到一个
未定义的变量:错误
而不是我创建的404页面。问题是,如果我尝试从有效的现有路由访问数据库中不存在的对象,则抛出404错误并且页面呈现它应该的样式。
我的错误页面的布局包括一个顶部栏,在该栏中有一个我在主布局中包含的模态表单(未定义的错误显然来自该模态):
mastererror.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
<!-- Load CSS -->
@include('assets.css')
<!-------------->
<!-- Load jQuery -->
<script src="{{ URL::asset('js/jquery-2.1.4.min.js')}}"></script>
<!----------------->
</head>
<body>
<!-- Load Navbar -->
@include('elements.html.navbartop')
<!----------------->
<div class="pusher">
<div id="content" class="ui main container" style="margin: 70px 0px 50px 0px">
<!-- Load Content -->
@yield('content')
<!------------------>
</div>
</div>
<!-- Load Footer -->
@include('elements.html.footer')
<!----------------->
<!-- Load JavaScript -->
@include('assets.js')
<!--------------------->
<!-- Load login Modal -->
@include('elements.html.partials.login-modal')
<!---------------------->
</body>
</html>
404.blade.php:
@extends('layout.mastererror')
@section('title', 'Not Found.')
@section('content')
<div style="margin-top: 30px">
<img class="ui medium image centered" src="{{asset('images/notfound.png')}}">
<h1 class="ui header centered">404 Not Found.</h1>
@if($exception->getMessage() !== '')
<h2 class="ui header centered">{{ $exception->getMessage() }}.</h2>
@else
<h2 class="ui header centered">The resource is missing.</h2>
@endif
<h3 class="ui header centered"><a href="{{ route('index') }}">Home</a> </h3>
</div>
@endsection
登录-modal.blade.php:
<div class="ui small modal" id="loginModal">
<i class="circular close icon inverted"></i>
<div class="header">
Login
</div>
<div class="ui active dimmer" id="loader" style="display: none">
<div class="ui text loader">Loading</div>
</div>
<div class="content">
<div class="ui grid">
<div class="eight wide column centered">
{!! Form::open(array('route' => 'auth.login', 'method' => 'post','id'=>'formLogin','class' => 'ui large form')) !!}
<div class="field {!! $errors->has('username') ? 'error' : '' !!}">
<div class="ui left icon input">
<i class="user icon"></i>
{!! Form::text('username','',['name'=>'username','id'=>'username','class' => 'pd','placeholder'=>'Pseudo']) !!}
</div>
{!! $errors->first('username', '<span class="ui text" id="" style="color: #bf4d4b">:message</span>') !!}
</div>
<div class="field {!! $errors->has('password') ? 'error' : '' !!}">
<div class="ui left icon input">
<i class="lock icon"></i>
{!! Form::password('password',['name'=>'password','id'=>'password','class' => '','placeholder'=>'Mot de passe']) !!}
</div>
{!! $errors->first('password', '<span class="ui text" id="" style="color: #bf4d4b">:message</span>') !!}
</div>
{!! Form::submit('Login',['id'=>'loginButton','class'=>'ui fluid large teal submit button']) !!}
{!! Form::close() !!}
<div class="ui message">
<p>Mot de passe oublié? {{ link_to_route('password.forgot','Réinitialiser',null,null) }}</p>
<p>Pas encore membre? {{ link_to_route('auth.register','s\'Enregistrer',null,null) }}</p>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
抛出错误时不会生成魔法$errors
变量(在此处详细讨论:https://github.com/laravel/framework/issues/11457)。
您需要对引用isset($errors)
$errors
进行操作
<div class="field {!! isset($errors) ? ( $errors->has('username') ? 'error' : '' ) : '' !!}">
答案 1 :(得分:0)
现在我添加了这条路线来捕捉任何无效路线:
Route::any('{catchall}', function($page){
abort(404,"the page '$page' doesnt exist");
})->where('catchall', '(.*)');