title: In Laravel 5, why is Request::root() different when called during phpunit test?
body: <p>I defined a test which tests the creation of a user. The controller is set to redirect back to the same page on error (using validation through a generated <code>App\Http\Requests\Request</code>). This works correctly when manually clicking in a browser, but fails during a test. Instead of being redirected to:</p>
  
  <pre><code>http://localhost/account/create
  </code></pre>
  
  <p>The test redirects to (missing a slash):</p>
  
  <pre><code>http://localhostaccount/create
  </code></pre>
  
  <p>Neither of these urls are what I have setup in the .htaccess or in the <code>$url</code> variable in config/app.php. Which is (On OSX Yosemite):</p>
  
  <pre><code>http://~username/laravel_projects/projectname/public
  </code></pre>
  
  <p>I finally pinpointed the issue to have something to do with how the result of <code>Request::root()</code> is generated. Making a call to this outside of a test results in the expected value defined in .htaccess and <code>$url</code>. Inside the test it results in:</p>
  
  <pre><code>http://localhost
  </code></pre>
  
  <p>What configuration needs to change in order to get this function to return the correct value in both contexts?</p>
  
  <p>I should also mention I made the painful upgrade from Laravel 4 to the current version 5.0.27. </p>
  
  <p>****** UPDATE *******</p>
  
  <p>I was able to figure out an acceptable solution/workaround to this issue!</p>
  
  <p>In Laravel 5, FormRequests were introduced to help move <a href="http://laravel.com/docs/master/validation" rel="nofollow">validation</a> logic out of controllers. Once a request is mapped to the controller, if a <code>FormRequest</code> (or just <code>Request</code>) is specified, this is executed before hitting the controller action.</p>
  
  <p>This <code>FormRequest</code> by default handles the response if the validation fails. It attempts to construct a redirect based on the route you posted the form data to. In my case, possibly related to an error of mine updating from Laravel 4 to 5, this default redirect was being constructed incorrectly. The <a href="https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php" rel="nofollow">Laravel System</a> code for handling the response looks like this:</p>
  
  <pre><code> /**
   * Get the proper failed validation response for the request.
   *
   * @param  array  $errors
   * @return \Symfony\Component\HttpFoundation\Response
   */
  public function response(array $errors)
  {
      if ($this-&gt;ajax() || $this-&gt;wantsJson())
      {
          return new JsonResponse($errors, 422);
      }
      return $this-&gt;redirector-&gt;to($this-&gt;getRedirectUrl())
                                      -&gt;withInput($this-&gt;except($this-&gt;dontFlash))
                                      -&gt;withErrors($errors, $this-&gt;errorBag);
  }
  </code></pre>
  
  <p>Notice how the returned redirect is NOT the same as calling <code>Redirect::route('some_route')</code>. You can <a href="https://mattstauffer.co/blog/laravel-5.0-form-requests" rel="nofollow">override</a> this <code>response</code> function by including <code>use Response</code> in your <code>Request</code> class.</p>
  
  <p>After using <code>Redirect::route()</code> to create the redirect, the logic in my tests passed with the expected results. Here is my Request code that worked:</p>
  
  <pre><code>namespace App\Http\Requests;
  
  use Illuminate\Foundation\Http\FormRequest;
  use App\Http\Requests\Request;
  use Response;
  
  class AccountRequest extends FormRequest {
  
      /**
       * Determine if the user is authorized to make this request.
       *
       * @return bool
       */
      public function authorize()
      {
          return true;
      }
  
      /**
       * Get the validation rules that apply to the request.
       *
       * @return array
       */
      public function rules()
      {
          return [
                    'email' =&gt; 'required|max:50|email|unique:users',
                    'password' =&gt; 'required|min:6',
                    'password_confirmation' =&gt; 'required|same:password'
  
                  ];
      }
  
    public function response(array $errors){
      return \Redirect::route('account_create');
    }
  
  }
  </code></pre>
  
  <p>The important part is that I called Redirect::route instead of letting the default response code execute.</p>