title: Laravel - Calling Redirect::to() from within view
body: <p>I'm currently working on a cms which is built on the Laravel 4 framework.  I'm trying to build a plugin system, similar to Pyro CMS, where module views can be included in a page view using the Blade template system.</p>
  
  <p>I'm building a contact form plugin that if submitted successfully will redirect the user to a given url, or simply redirect back to the existing page.</p>
  
  <p>The code for my contact form class is:</p>
  
  <pre><code>class Contact {
  
  public static function form($params)
  {
      //get params and execute relevant logic here
  
      $redirect = isset($params['redirect']) ? $params['redirect'] : Request::url();
  
      $data = Input::all();
  
      if($data)
      {
          // Run validation and send message here
          return Redirect::to($redirect)
      }
  
      return View::make('contact_form_view');
  }
  
  }
  </code></pre>
  
  <p>There is a page controller that will display the appropriate page view depending on the route used and the idea is that a user can drop a contact form into any page template and easily customise it by calling the form function from within the template view as shown below</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          {{ Contact::form(array(
  
              'to' =&gt; 'myemail@mydomain.com',
              'view'  =&gt; 'contact_form_1',
  
          )) }}
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This all works fine apart from the redirect. When the form is successfully submitted and the message sent the page refreshes and displays the following message in place of the contact form</p>
  
  <pre><code>HTTP/1.0 302 Found Cache-Control: no-cache Date: Tue, 17 Sep 2013 09:14:16 GMT Location: http://localhost:8888/my_initial_route Redirecting to http://localhost:8888/my_new_route.
  </code></pre>
  
  <p>About 2 seconds later the redirect then takes place and the browser is redirected to whatever route the user provides in the $params array, or to the current page if no redirect is provided, as in the example above.</p>
  
  <p>Any suggestions as to why it is doing this, as well as any solutions, would be great.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is a really brief overview of why I have taken the above approach, which might help with understanding the problem:</p>
  
  <p>A requirement of the project is that the developers that will eventually use the cms can create a indefinite amount of pages via the cms control panel by clicking an "Add page" button.  The idea is that we aren't writing new page controllers for every new page we want to create. With that I mind I have the following route:</p>
  
  <pre><code>Route::any('{page_slug}/page', array('as' =&gt; 'frontend.page', 'uses' =&gt; 'FrontendPagesController@display_page'))-&gt;where('page_slug',  '[-A-Za-z0-9_-]+');
  </code></pre>
  
  <p>The <code>display_page</code> function slimmed greatly is:</p>
  
  <pre><code>public function display_page($slug)
  {
      $page = PagesModel::getPageBySlug($slug);
      return View::make($page['view_name']);
  }
  </code></pre>
  
  <p>With this users can create a page with the click of a button, give it a name and a slug, and the app can then display that page and the corresponding view -- defined when creating the page -- without having to add any new routes, controllers etc.</p>
  
  <p>The problem arrises in a situation where a user wants to include a contact form with a page. How do we know if they will add a form or not, what fields they will use and what validation it will require? I need a way of being able to add a completely custom contact form into any page without the developer having to touch touch the cms code. My solution to this was the approach given above: the developer drops the tag into the view and passes the function some params, which customise the forms functionality.</p>
  
  <p>The <code>Contact::form()</code> function is kind of like a controller for the form. It gets and returns the view that contains the from html, processes the submission and returns success/errors messages depending on the outcome.</p>
  
  <p>Now I don't necessarily need to use the approach above but I need a way of separating the contact form controller from the page controller and for developers to be able to add a form to any page without touching the cms backend code.</p>
  
  <p>Any ideas on this would be great.</p>