Cakesuit/Option plugin for CakePHP
What is it ?
This plugin allows you to simply define options for your application with the key/value pair., (*1)
Requirements
Installation
You can install this plugin into your CakePHP application using composer., (*2)
The recommended way to install composer packages is:, (*3)
composer require cakesuit/option
Load plugin in config/bootstrap.php application, (*4)
bin/cake plugin load Cakesuit/Option
If you want to use the built-in routes, load the plugin as follows, (*5)
bin/cake plugin load -r Cakesuit/Option
Migration
Now that the plugin is installed and loaded, you have to migrate the schema for the database, (*6)
bin/cake migrations migrate -p Cakesuit/Option
Default Routes
<?php
Router::plugin(
'Cakesuit/Option',
['path' => '/cakesuit'],
function (RouteBuilder $routes) {
// List all
$routes->connect(
'options',
[
'controller' => 'Options',
'action' => 'index'
]
);
// Add
$routes->connect(
'options',
[
'controller' => 'Options',
'action' => 'add'
]
);
// Edit
$routes->connect(
'options/edit/:id',
[
'controller' => 'Options',
'action' => 'edit',
[
'id' => '[0-9]+',
'pass' => ['id']
]
]
);
// Show
$routes->connect(
'options/view/:id',
[
'controller' => 'Options',
'action' => 'view',
[
'id' => '[0-9]+',
'pass' => ['id']
]
]
);
// Delete
$routes->connect(
'options/delete/:id',
[
'controller' => 'Options',
'action' => 'delete',
[
'id' => '[0-9]+',
'pass' => ['id']
]
]
);
}
);
Database
Here is an example of what can be found in the table, (*7)
key |
value |
autoload |
site_name |
My Blog |
1 |
site_description |
Use CakeSuit/Option for advance settings |
1 |
analytics_ua |
UA-XXXXXX |
0 |
How to us ?
Recover all data marked autoload, (*8)
<?php
// Load the Model if necessary
$this->loadModel('Options');
// Fetch all data marked autoload
$options = $this->Options->find('autoload');
echo $options->site_name; // = 'My Blog'
echo $options->site_description; // = 'Use CakeSuit/Option for advance settings'
echo $options->analytics_ua; // = null (this value does not exist)
Recover data by keys, (*9)
<?php
// Load the Model if necessary
$this->loadModel('Cakesuit/Option.Options');
$options = $this->Options->find('keys', [
'keys' => ['analytics_ua', 'site_description']
]);
echo $options->site_name; // = null
echo $options->site_description; // = 'Use CakeSuit/Option for advance settings'
echo $options->analytics_ua; // = 'UA-XXXXXX'
Check if empty data, (*10)
<?php
// Load the Model if necessary
$this->loadModel('Cakesuit/Option.Options');
$options = $this->Options->find('keys', [
'keys' => ['no_exists_value']
]);
$options->isEmpty(); // = true
Count data, (*11)
<?php
// Load the Model if necessary
$this->loadModel('Cakesuit/Option.Options');
$options = $this->Options->find('autoload');
echo $options->count(); // = 2
...
If you encounter any difficulties, contact me.
Thank you., (*12)
C@kesuit, (*13)