【问题标题】:(Post/Redirect/Get pattern) Laravel layout variables don't work after redirect(发布/重定向/获取模式)Laravel 布局变量在重定向后不起作用
【发布时间】:2013-11-26 09:08:45
【问题描述】:

我在我的 Laravel 控制器中使用 Post/Redirect/Get (PRG) 模式来防止重复提交表单。

当我不使用布局或我的布局不使用任何变量时,它运行良好。问题是我的布局使用了一个名为$title 的变量。当我加载视图和布局而不重定向它运行良好时,控制器中设置的标题被传递给布局,但是在处理表单并重定向到使用相同布局和相同控制器方法的相同路由后,我得到一个“未定义的变量:标题”来自我的布局文件的错误。

这是我的代码:

文件:app/routes.php

Route::get('contact', array('as' => 'show.contact.form', 'uses' => 'HomeController@showContactForm'));
Route::post('contact', array('as' => 'send.contact.email', 'uses' => 'HomeController@sendContactEmail'));

文件:app/controllers/HomeController.php

class HomeController extends BaseController {

    protected $layout = 'layouts.master';

    public function showContactForm()
    {
        $this->layout->title = 'Contact form';
        $this->layout->content = View::make('contact-form');
    }

    public function sendContactEmail()
    {
        $rules = ['email' => 'required|email', 'message' => 'required'];
        $input = Input::only(array_keys($rules));
        $validator = Validator::make($input, $rules);

        if($validator->fails())
            return Redirect::back()->withInput($input)->withErrors($validator);

        // Code to send email omitted as is not relevant

        Redirect::back()->withSuccess('Message sent!');
    }
}

文件:app/views/layouts/master.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>{{{ $title }}}</title>
    </head>
    <body>
        @yield('body')
    </body>
</html>

文件:app/views/contact-form.blade.php

@section('body')
    @if (Session::has('success'))
        <div class="success">{{ Session::get('success') }}</div>
    @endif

    {{
        Form::open(['route' => 'send.contact.email']),
        Form::email('email', null, ['placeholder' => 'E-mail']),
        Form::textarea('message', null, ['placeholder' => 'Message']),
        Form::submit(_('Send')),
        Form::close()
    }}
@stop

我不明白为什么重定向后下一行代码被忽略

$this->layout->title = 'Contact form';

我试过Redirect::action('HomeController@sendContactEmail');Redirect::route('show.contact.form'); 但结果是一样的。

负责渲染那个view的controller在redirect前和redirect后是一模一样的,而且完全没有业务逻辑,为什么只适用于第一种情况而不能适用于第二种情况呢?

【问题讨论】:

    标签: layout laravel laravel-4 post-redirect-get


    【解决方案1】:

    您是否尝试过使用

    return View::make('contact-form', array('title' => 'Contact Form'));
    

    而不是直接与布局交互?

    【讨论】:

      【解决方案2】:

      这个

      Redirect::back()->withSuccess('Message sent!');
      

      应该是

      return Redirect::back()->withSuccess('Message sent!');
      

      当在控制器中设置layout 属性并且方法没有返回任何响应时,控制器会尝试呈现layout。在您的sendContactEmail() 方法中,两个条件都满足并且控制器尝试在设置$title 之前呈现布局。

      Illuminate\Routing\Controllers\controller 中查看callAction()

      http://laravel.com/api/source-class-Illuminate.Routing.Controllers.Controller.html#93-127

      【讨论】:

        【解决方案3】:

        Redirect::back() 使用当前 HTTP 请求的引用值创建 302。我将首先将初始表单请求与重定向请求进行比较,看看是否会产生任何线索。你也可以试试……

        Redirect::route('HomeController@showContactForm')->withInput()...
        

        我知道它的动态性较低,但它会生成 URL,而不是依赖 HTTP 标头中的引用者值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-27
          • 2020-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多