my cookie is null! But I don't know :
use Illuminate\Support\Facades\Cookie;
HomeController:
public function index()
{
Cookie::queue('currentLang', 'heb', 999999999);
$cat1 = $this->categoryRepo->findCategoryById(1);
$lastPosts = $this->blogPosts->listBlogPosts(array('*'),'id','desc')->take(3);
return view('front.index', compact('cat1','lastPosts'));
}
now I want to get this key from another controller:
LoginController:
public function showLoginForm()
{
dd(Cookie::get('currentLang'));
return view('auth.login');
}
but it returns null ! I'm working on localhost.
Cookie::queue(...
is used to attach a cookie to your response(an alternative), per the docs. If you inspect the response headers you should notice that Set-Cookie: currentLang=someencryptedvalue
does indeed exist.
I have created some prototype endpoints to illustrate creating and deleting cookies:
use Illuminate\Support\Facades\Cookie;
Route::get('/wont-clear-cookie', function() {
Cookie::forget('currentLang');
return redirect()->to('/get-cookie');
});
Route::get('/clear-cookie', function() {
Cookie::queue(Cookie::forget('currentLang'));
return redirect()->to('/get-cookie');
});
Route::get('/set-cookie', function() {
Cookie::queue('currentLang', 'heb', 999999999);
return redirect()->to('/get-cookie');
});
Route::get('/get-cookie', function() {
dump(Cookie::get('currentLang'));
dd(request()->cookie('currentLang'));
});
The above endpoints should be placed in web.php
.
Also, Make sure your web
middleware group is configured correctly in App\Http\Kernal.php
:
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],