dongshanjin8947 2015-01-02 11:30
浏览 94
已采纳

withX()在Laravel 4.2中不起作用

The problem is that withErrors() is working perfectly in this code, but withMessage() isn't.

I also tried with('message', 'Test message!'). In views file, I can retrieve withErrors using $errors variable, but if I want to retrieve withMessage, I have to use Session::get('message'). Why is $message not working?

Controller:

public function registration() {
    $rules = array(...);
    $validator = Validator::make(Input::all(), $rules);

    if($validator->fails()) {
        return Redirect::route('registration')->withErrors($validator);
    }
    else {
        //Some code here...

        return Redirect::route('registration')->withMessage('Test message!');
    }
}

Template:

@extends('default.base')

@section('main')
    @if(!empty($errors->all()))
        <div class="alert alert-danger" role="alert">
            <ul>
                @foreach($errors->all() as $error)
                    <li>{{ $error }}
                @endforeach
            </ul>
        </div>
    @endif


    @if(isset($message))
    {{ $message }}
    @endif
@stop
  • 写回答

1条回答 默认 最新

  • dttl3933 2015-01-02 11:39
    关注

    That is because errors is a special case. When creating a view, Laravel check's if there is a session variable with the name errors. If so it will then pass the contents as $errors to the view.

    Illuminate\View\ViewServiceProvider@registerSessionBinder

    if ($me->sessionHasErrors($app))
    {
        $errors = $app['session.store']->get('errors');
    
        $app['view']->share('errors', $errors);
    }
    

    This means you either use Session::has('message') and Session::get('message') in your view or you add a View Composer that does basically the same that Laravel does with errors:

    View::composer('*', function($view){
        if(Session::has('message')){
            $view->with('message', Session::get('message'));
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器