elle-the-dev/oatmeal
最新稳定版本:v1.0.0
Composer 安装命令:
composer require elle-the-dev/oatmeal
包简介
PHP cookie management in a modern, object-oriented manner.
关键字:
README 文档
README
Oatmeal is a cookie manager for modern, object-oriented PHP development. Oatmeal works both as a standalone package, as well as having built-in integration into Laravel.
Requirements
Oatmeal requires PHP 7.0 and higher.
Installation
Installation is done via composer
composer require elle-the-dev/oatmeal
If we want to integrate into Laravel, we'll additionally want to run
php artisan vendor:publish
This will publish the config file to config/oatmeal.php.
Also for Laravel, if we don't use auto-discovery, we'll need to add OatmealServiceProvider to the providers array in config/app.php
ElleTheDev\Oatmeal\Providers\OatmealServiceProvider::class,
Usage
Instantiation
Ideally we'll be using Oatmeal with a side of dependency injection, but this is what it looks like to instantiate a new instance on its own.
$oatmeal = new ElleTheDev\Oatmeal\Oatmeal;
Configuration can be passed in via the constructor so we don't need to specify path, domain, https and http-only settings each time we want to set a cookie.
$oatmeal = new ElleTheDev\Oatmeal\Oatmeal([ 'path' => '/', 'domain' => 'example.com', 'secure' => false, 'httpOnly' => true, ]);
In Laravel, the service provider allows for auto-injecting the Oatmeal interface and autoloading the config from config/oatmeal.php
use ElleTheDev\Oatmeal\Contracts\Oatmeal; class ExampleController extends Controller { public function show(Oatmeal $oatmeal) { } }
When injecting in Laravel, Oatmeal will automatically be loaded using the config file config/oatmeal.php.
Setting
We can set a basic cookie with a timeout in minutes. set will also update the $_COOKIE superglobal array with the new cookie value.
$oatmeal->set('name', 'value', 60);
We can also store a cookie with no expiration (note: it technically expires in ~5 years)
$oatmeal->forever('name', 'value');
If we need to change settings before setting a cookie, we can do so through chained method calls.
$oatmeal->setPath('/') ->setDomain('example.com') ->setSecure(false) ->setHttpOnly(true) ->set('name', 'value', 60);
Getting
Getting a cookie is as simple as calling get. If the requested cookie does not exist or is expired, get will return null
$value = $oatmeal->get('name');
If we'd like to forget the cookie after getting it, we can pull instead. pull will also update the $_COOKIE superglobal to remove the pulled cookie.
$value = $oatmeal->pull('name');
Deleting
We can delete a cookie using forget
$oatmeal->forget('name');
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-20