Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Post/Redirect/Get pattern) Laravel layout variables don't work after redirect
    primarykey
    data
    text
    <p>I'm using the <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get" rel="nofollow">Post/Redirect/Get (PRG)</a> pattern in my Laravel controllers to prevent duplicate form submission.</p> <p>It works well when I don't use layouts or when my layouts don't use any variable. The problem is my layout uses a variable named <code>$title</code>. When I load the view and the layout without redirect it works well, the title set in the controller is passed to the layout, but after processing a form and redirecting to the same route which uses the same layout and the same controller method I get a <strong>"Undefined variable: title"</strong> error coming from my layout file.</p> <p>Here is my code:</p> <p>File: app/routes.php</p> <pre><code>Route::get('contact', array('as' =&gt; 'show.contact.form', 'uses' =&gt; 'HomeController@showContactForm')); Route::post('contact', array('as' =&gt; 'send.contact.email', 'uses' =&gt; 'HomeController@sendContactEmail')); </code></pre> <p>File: app/controllers/HomeController.php</p> <pre><code>class HomeController extends BaseController { protected $layout = 'layouts.master'; public function showContactForm() { $this-&gt;layout-&gt;title = 'Contact form'; $this-&gt;layout-&gt;content = View::make('contact-form'); } public function sendContactEmail() { $rules = ['email' =&gt; 'required|email', 'message' =&gt; 'required']; $input = Input::only(array_keys($rules)); $validator = Validator::make($input, $rules); if($validator-&gt;fails()) return Redirect::back()-&gt;withInput($input)-&gt;withErrors($validator); // Code to send email omitted as is not relevant Redirect::back()-&gt;withSuccess('Message sent!'); } } </code></pre> <p>File: app/views/layouts/master.blade.php</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;{{{ $title }}}&lt;/title&gt; &lt;/head&gt; &lt;body&gt; @yield('body') &lt;/body&gt; &lt;/html&gt; </code></pre> <p>File: app/views/contact-form.blade.php</p> <pre><code>@section('body') @if (Session::has('success')) &lt;div class="success"&gt;{{ Session::get('success') }}&lt;/div&gt; @endif {{ Form::open(['route' =&gt; 'send.contact.email']), Form::email('email', null, ['placeholder' =&gt; 'E-mail']), Form::textarea('message', null, ['placeholder' =&gt; 'Message']), Form::submit(_('Send')), Form::close() }} @stop </code></pre> <p>I don't understand why after redirecting the next line of code is ignored</p> <pre><code>$this-&gt;layout-&gt;title = 'Contact form'; </code></pre> <p>I've tried with <code>Redirect::action('HomeController@sendContactEmail');</code> or <code>Redirect::route('show.contact.form');</code> but the result is the same. </p> <p><strong>The controller in charge of rendering that view is exactly the same before the redirect than after the redirect, and it has no business logic at all, so why it only works on the first case but not in the second?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload