Laravel 5.2 Flash消息和错误突然无法正常工作

时间:2017-08-03 19:37:50

标签: php laravel validation laravel-5

最近几天,我遇到了问题。验证错误变量在刀片文件中是空的..我是 在Laravel 5.2.45中开发多语言应用程序。所以每个都有验证错误消息(resources / lang / {locale} /validation.php)。

验证规则存在于包含验证规则的Request文件(例如ValidateUserRequest)中,并声明authorize为true。然后我将它传递给控制器​​。 Bellow是我的中间件,也是我路线文件的一部分。

非常感谢任何帮助

Kernel.php

 protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
 ];

 protected $routeMiddleware = [
    'auth_pabl' => \App\Http\Middleware\AuthenticateBackEnd::class,
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
 ];

routes.php(管理员目录中的所有控制器)

Route::auth();

Route::group ( [ 
    'namespace' => 'Admin'
], 
function () {

Route::get ( 'admin/dashboard', [ 

        'uses' => 'PanelController@dashboard',
        'as' => 'admin.dashboard'

] );

/*
 * Authors
 */

Route::get ( 'admin/authors', [ 

        'uses' => 'PanelController@authors',
        'as' => 'admin.authors'

] );

Route::post ( 'admin/authors', [ 

        'uses' => 'AuthorController@store',
        'as' => 'admin.authors.store' 
] );

Route::get ( 'admin/authors/{slug}/edit', [ 

        'uses' => 'AuthorController@edit',
        'as' => 'admin.authors.edit' 
] );

Route::post ( 'admin/authors/{username}/edit', [ 

        'uses' => 'AuthorController@update',
        'as' => 'admin.authors.update' 
] );

Route::get ( 'admin/authors/{username}/delete', [ 

        'uses' => 'AuthorController@delete',
        'as' => 'admin.authors.delete' 
] );

会话消息通常来自

\Session::flash('flash_contact', 'Success. Data stored');

return redirect()->route('admin.events.create');

它(不)用

显示
@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
    <p>{!! trans('admin/form/placeholders.errors') !!}</p>
    <ul class="errors">
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    </ul>
</div>
@endif
@if(Session::has('flash_contact'))

<div id="success" class="alert alert-success text-center">
    {{Session::get('flash_contact')}}
</div>
@endif

最后php artisan route:list表示“web”中间件存在(我没有手动声明它两次)

//编辑1 (带有来自控制器的错误)

请注意,两个函数withErrors都没有给出预期的结果。下面是我的控制器的一部分。

public function store(Requests\ValidateEventsRequest $request)
{

    try {
        return $this->dbEvent->add($request);
    } catch (\Exception $e) {

        return redirect()->to('/admin/events/add')->withErrors('Error 
            detected');
    }

}

//编辑2

这不应该起作用吗?

Route::get('flash', function () {

    return redirect()->to('flash2')->withErrors('Where are my errors?');

});

Route::get('flash2', function () {

    return view('flash2');

});

我的flash2.blade.php

<html>
<body>
@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
    <p>{!! trans('admin/form/placeholders.errors') !!}</p>
    <ul class="errors">
    @foreach($errors->all() as $error)
        <li>{{$error}}</li>
    @endforeach
    </ul>
</div>
@endif
this is my flash page
</body>
</html>

1 个答案:

答案 0 :(得分:0)

要根据我的理解访问$errors,您必须将其添加到ErrorBag中,所以请执行此操作:

return redirect()->route('admin.events.create')->withErrors('not_found', $e->getMessage);

然后你可以访问$errors变量并在视图中找到它。

<强>更新 对于那些可能在谷歌搜索结果中找到这个答案的人:

基于这两个答案:herehere以及OP的确认:

  

将Illuminate \ Session \ Middleware \ StartSession ::类移动到$ routeMiddleware是根据链接的解决方案并且它有效。

我希望Laravel已经修复了这个问题,我认为是这样,因为网络路线和其他路线现在已经在L5.4中分开了。