Leaf Cookie
Cookies are small pieces of text sent to a client's browser by your application. They help your app remember information about users' visits, which can both make it easier to visit your app and make it more useful to your users.
The cookie module helps you create, interact with and manage your cookies.
Installation
You can quickly install leaf cookies with composer or leaf cli.
leaf install cookie
or with composer:
composer require leafs/cookie
Usage
Right after installing the cookie module, you can start using it on the cookie()
method like this:
cookie()->set('name', 'Fullname');
Leaf cookie provides a Leaf\Http\Cookie
class for quickly using cookie methods:
use Leaf\Http\Cookie;
...
Cookie::set('name', 'Fullname');
Setting Cookies
The cookie module provides 3 methods for setting cookies:
set()
simpleCookie()
response()->withCookie
Set
This method allows you to set a cookie which should be returned with your next response to the client. It takes in 3 params:
- cookie name (string|array)
- cookie value (optional - string)
- cookie options (optional - array)
// normal method
cookie()->set('name', 'Fullname');
// using array
cookie()->set(['name' => 'Fullname']);
// normal method
Cookie::set('name', 'Fullname');
// using array
Cookie::set(['name' => 'Fullname']);
You can also set multiple cookies at a time
cookie()->set([
'name' => 'Fullname',
'age' => '18'
]);
Cookie::set([
'name' => 'Fullname',
'age' => '18'
]);
Cookies can also be set with options. These options allow you to set the cookie's expiry time, path, domain, secure and httponly. They determine how long the cookie should last and who should have access to it.
cookie()->set('name', 'Fullname', ['expire' => 0]);
Cookie::set('name', 'Fullname', ['expire' => 0]);
Options for cookies are:
- expire
- path
- domain
- secure
- httponly
simpleCookie
This method allows you to quickly set a cookie and it's expiry time. It takes in 3 params:
- cookie name (string|array)
- cookie value (optional - string)
- cookie expiresAt (optional - string - default of 7 days)
cookie()->simpleCookie('name', 'Fullname', '2 days');
Cookie::simpleCookie('name', 'Fullname', '2 days');
response()->withCookie
This method allows you to set a cookie directly on the response object. It takes in 3 params:
- cookie name (string)
- cookie value (string)
- cookie expiresAt (optional - string - default of 7 days)
response()->withCookie('name', 'Fullname', '2 days')->json([
'message' => 'Cookie set'
]);
$app
->response
->withCookie('name', 'Fullname', '2 days')
->json([
'message' => 'Cookie set'
]);
Getting Cookies
Just as you can set cookies, you can also get them from the client. The cookie module provides 2 methods for retrieve cookies:
get()
all()
get
get()
returns a particular set cookie
$name = cookie()->get('name');
$name = Cookie::get('name');
all
all()
returns all set cookies.
$cookies = cookie()->all();
$cookies = Cookie::all();
Deleting Cookies
The cookie module provides 3 methods for deleting cookies:
response()->withoutCookie()
unset()
unsetAll()
response()->withoutCookie
This method allows you to delete a cookie directly on the response object. It takes in 1 param which is the cookie to delete.
response()->withoutCookie('name')->json([
'message' => 'Cookie deleted'
]);
$app
->response
->withoutCookie('name')
->json([
'message' => 'Cookie deleted'
]);
unset
This method allows you to delete a cookie that was previously set. It takes in the cookie to unset.
cookie()->unset('name');
Cookie::unset('name');
You can also unset multiple cookies at a time
cookie()->unset(['name', 'age']);
Cookie::unset(['name', 'age']);
unsetAll
This method removes all set cookies.
cookie()->unsetAll();
Cookie::unsetAll();