CakePHP 4 Site Configuration Using Database
This tutorial will show the step to create a site configuration that is accessible from the form interface and stored in a database table: configurations in CakePHP 4. Basically, this site configuration leverage the global variable configuration to make it accessible throughout the system.
This tutorial may be outdated
It was last reviewed 4 years ago. Some steps may differ in newer versions.
Tech stack
- CakePHP 4.x
This tutorial will show the step to create a site configuration that is accessible from the form interface and stored in a database table: configurations in CakePHP 4. Basically, this site configuration leverage the global variable configuration to make it accessible throughout the system.
Create Database Table: Configurations
Create the database table name configurations which contain the site configuration. You can add or remove any items and change the value according to your requirement. This is the sample of the database table and the value I used in this tutorial.
SQL dump:
1CREATE TABLE `configurations` (
2 `id` char(36) NOT NULL,
3 `system_name` varchar(255) NOT NULL,
4 `system_abbr` varchar(255) NOT NULL,
5 `organization_name` varchar(255) NOT NULL,
6 `email` varchar(100) NOT NULL,
7 `meta_title` varchar(255) NOT NULL,
8 `meta_keyword` varchar(255) NOT NULL,
9 `meta_desc` varchar(255) NOT NULL,
10 `timezone` varchar(100) NOT NULL,
11 `author` varchar(255) NOT NULL,
12 `user_reg` tinyint(1) NOT NULL,
13 `version` varchar(255) NOT NULL,
14 `created` datetime NOT NULL,
15 `modified` datetime NOT NULL
16) ENGINE=InnoDB DEFAULT CHARSET=latin1;
17
18INSERT INTO `configurations` (`id`, `system_name`, `system_abbr`, `organization_name`, `domain_name`, `email`, `meta_title`, `meta_keyword`, `meta_desc`, `timezone`, `author`, `user_reg`, `version`, `created`, `modified`) VALUES
19('myCake4', 'My CakePHP 4', 'MC4', 'Code The Pixel', 'noreply[at]yourDomain.com', 'Test', 'Test', 'Test', 'Asia/Kuala_Lumpur', 'Code The Pixel', 1, '1.0', '2020-04-08 20:56:04', '2020-05-16 21:02:28');Load Configuration Model
Next, load the configuration model into AppController to make it accessible from all controllers. Then set the global variables for each of the attributes as shown below:
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
$this->loadModel('Configurations');
$settings = $this->Configurations->find('all')->first();
$this->set('system_name', $settings->get('system_name'));
$this->set('system_abbr', $settings->get('system_abbr'));
$this->set('organization_name', $settings->get('organization_name'));
$this->set('email', $settings->get('email'));
$this->set('meta_title', $settings->get('meta_title'));
$this->set('meta_keyword', $settings->get('meta_keyword'));
$this->set('meta_desc', $settings->get('meta_desc'));
$this->set('timezone', $settings->get('timezone'));
$this->set('author', $settings->get('author'));
$this->set('user_reg', $settings->get('user_reg'));
$this->set('version', $settings->get('version'));
}You can call or print the configuration value from any model in your application (eg: print the system_name value):
<?= $system_name; ?>To make it more interesting, bake the configurations table and remove all the others function except edit. Then rename the public function edit to public function setting (obviously it will be in your ConfigurationsController.php). Then set the form to redirect to setting back once updated the value as shown below:
public function setting($id = null)
{
$configuration = $this->Configurations->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$configuration = $this->Configurations->patchEntity($configuration, $this->request->getData());
if ($this->Configurations->save($configuration)) {
$this->Flash->success(__('The configuration has been updated.'));
return $this->redirect(['action' => 'setting', 'myCake4']);
}
$this->Flash->error(__('The configuration could not be update. Please, try again.'));
}
$this->set(compact('configuration'));
}Then navigate to routes and add the following codes to manage the redirect. Now you can access your setting /configuration page through localhost/myCake4/config (Note: myCake4 is the ID of the setting. If you change the value to another value in the database, then use it consistently)
$builder->connect('/config/*', ['controller' => 'Configurations', 'action' => 'setting','myCake4']);Note: The shown image differs from your stock generated view.
Now you've your own site configuration page which is very useful for other non-programmer or technical people to update the site configuration eg: change the corresponding email, system name etc. That all. Happy coding :)
Did this tutorial help?
210 readers found this helpful
Comments
No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.