Unable to set cookies in Laravel 4

I am using the latest version of Laravel 4 and I cannot set cookies:

Route::get('cookietest', function()
{
    Cookie::forever('forever', 'Success');
    $forever = Cookie::get('forever');
    Cookie::make('temporary', 'Victory', 5);
    $temporary = Cookie::get('temporary');
    return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});

Show script:

@extends('layouts.master')

@section('content')
    Forever cookie: {{ $forever }} <br />
    Temporary cookie: {{ $temporary }} <br />
    Variable test: {{ $variableTest }}
@stop

Productivity:

Forever cookie: 
Temporary cookie: 
Variable test: works

It doesn’t matter if I refresh the page or create cookies in one route and try to access them in another. I can confirm that no cookies are set with the above operation. The cookies "laravel_payload" and "laravel_session", as well as "remember_ [HASH]" exist, and I can set cookies using regular PHP using setcookie.

No errors or errors were found in any place that I can find. I am running Linux Mint locally and Debian on my server, with both nginx and the same problem in both places.

+5
source share
3

Cookies , , . , .

,

Cookie::forever('cookie', 'value');
$cookie = Cookie::get('cookie');

, cookie .

, ,

Route::get('cookieset', function()
{
    $foreverCookie = Cookie::forever('forever', 'Success');
    $tempCookie = Cookie::make('temporary', 'Victory', 5);
    return Response::make()->withCookie($foreverCookie)->withCookie($tempCookie);
});


Route::get('cookietest', function()
{
     $forever = Cookie::get('forever');
     $temporary = Cookie::get('temporary');
     return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});

yoursite.local/cookieset, yoursite.local/cookietest, , , cookie .

+16

Laravel 4 cookie queue.

// Set a cookie before a response has been created
Cookie::queue('key', 'value', 'minutes');

:

Cookie::queue('username', 'mojoman', 60 * 24 * 30); // 30 days

. Laravel 3 put (http://v3.golaravel.com/api/class-Laravel.Cookie.html#_put).

:

Cookie::put('username', 'mojoman', 60 * 24 * 30); // 30 days
+15

AfterFilter can be used to set cookies in the controller. Assuming the cookie is stored in the $ cookie controller class variable. In the controller’s constructor, the following code automatically inserts a cookie into any view returned to the client:

public function __construct () {
    $cookie = &$this->cookie;
    $this->afterFilter(function ($route, $request, $response) use(&$cookie)  {
            if ($cookie) {
                $response->withCookie( $cookie );
            }
    });
}
0
source

All Articles