06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

HTTP Requests 154<br />

1 <br />

Cookies<br />

Retrieving Cookies From The Request<br />

All cookies created by the Laravel framework are encrypted and signed with an authentication code,<br />

meaning they will be considered invalid if they have been changed by the client. To retrieve a cookie<br />

value from the request, you may use the cookie method on the Illuminate\Http\Request instance:<br />

1 $value = $request->cookie('name');<br />

Attaching A New Cookie To A Response<br />

Laravel provides a global cookie helper function which serves as a simple factory for generating<br />

new Symfony\Component\HttpFoundation\Cookie instances. The cookies may be attached to a<br />

Illuminate\Http\Response instance using the withCookie method:<br />

1 $response = new Illuminate\Http\Response('Hello World');<br />

2<br />

3 $response->withCookie('name', 'value', $minutes);<br />

4<br />

5 return $response;<br />

To create a long-lived cookie, which lasts for five years, you may use the forever method on the<br />

cookie factory by first calling the cookie helper with no arguments, and then chaining the forever<br />

method onto the returned cookie factory:<br />

1 $response->withCookie(cookie()->forever('name', 'value'));

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!