【问题标题】:Laravel 5.2 flash messages and errors suddenly not workingLaravel 5.2 flash 消息和错误突然不起作用
【发布时间】:2018-01-11 14:06:30
【问题描述】:

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

验证规则位于包含验证规则并将授权声明为 true 的请求文件(例如 ValidateUserRequest)中。然后我将它传递给控制器​​。 Bellow 是我的中间件和路由文件的一部分。

任何帮助将不胜感激

内核.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(Admin 目录内的所有控制器)

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 from controller)

请注意,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>

【问题讨论】:

  • 你试过转储错误吗?有可能没有错误..
  • @loannisKaragiannis。我认为这可能是您的解决方案:stackoverflow.com/a/36691774/5704410
  • 不幸的是没有.. $errors 仍然是空的。 protected 'bags' => array (size=0) empty 。另外,web 中间件现在通过了两次,在我的 route:list
  • @flex_ 倾倒错误给我一个空包
  • @OmisakinOluwatobi 你能看看Edit 2..吗?即使是这个简单的测试也没有按预期返回我的 $errors

标签: php laravel validation laravel-5


【解决方案1】:

据我了解,为了访问 $errors,您必须将其添加到 ErrorBag 中,因此您可以这样做:

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

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

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

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

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

我希望这已经在 Laravel 中修复了,我想是的,因为 web 路由和其他路由现在在 L5.4 中是分开的。

【讨论】:

  • 如果您使用 ShareErrorsFromSession 中间件,$errors 始终可用。 laravel.com/docs/5.4/…
  • 是的,但是在这种情况下,如果您不自己添加错误包中的“错误”项,则不会进入该项。我可能是错的,但前提是这不是您面临的问题
  • 我的坏.. withErrors 也不能正常工作.. 我会更新我的帖子
  • 请原谅我的错误,是的,$errors 在验证完成后始终可用,但我认为使用\Session::flash('errors', $e-&gt;getMessage()); 不会将此错误传递给$errors 变量。
  • 我怀疑它与我的中间件“auth_pabl”有关。不过它曾经可以工作..我也相信 withErrors('Error message') 没问题..只是一个将添加到 $errors 数组中的字符串。到目前为止,我以这种方式使用它没有任何问题。
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2017-05-14
  • 1970-01-01
相关资源
最近更新 更多