{"id":1097,"date":"2022-03-10T09:21:46","date_gmt":"2022-03-10T09:21:46","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1097"},"modified":"2024-02-12T19:08:34","modified_gmt":"2024-02-12T19:08:34","slug":"plugin-load-custom-config","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/plugin-load-custom-config\/","title":{"rendered":"Loading custom config for a CakePHP plugin"},"content":{"rendered":"<p>Say you're creating a plugin that integrates some <code>ExampleAPI<\/code> and you need to store the API client configuration. You will have to create a custom config file inside your plugin, then tell the plugin bootstrap to load the config file. Finally, make sure you're loading your plugin with the <code>bootstrap<\/code> flag.<\/p>\n<ol>\n<li>\n<p>Create your custom config file under <code>plugins\/ExamplePlugin\/config\/ExampleAPI.php<\/code> with the following content:<\/p>\n<pre><code><?php\n\nreturn [\n    'ExampleAPI' => [ \/\/ group\n        'host' => '',\n        'username' => '',\n        'key' => '',\n    ],\n];<\/code><\/pre>\n<\/li>\n<li>\n<p>Create the plugin bootstrap file <code>plugins\/ExamplePlugin\/config\/bootstrap.php<\/code>:<\/p>\n<pre><code><?php\n\n\/\/ The first ExampleAPI before the dot is for the plugin name\n\/\/ The second ExampleAPI corresponds with the config group above\n\\Cake\\Core\\Configure::load('ExampleAPI.ExampleAPI', 'default');<\/code><\/pre>\n<\/li>\n<li>\n<p>In your <code>src\/Application.php<\/code>:<\/p>\n<blockquote>\n<p><strong>Note<\/strong>: <code>&#039;bootstrap&#039; =&gt; true<\/code> is required<\/p>\n<\/blockquote>\n<pre><code>$this->addPlugin('ExampleAPI', ['bootstrap' => true, 'routes' => false]);\n\/\/ ExampleAPI matches the plugin name\n\/\/ the bootstrap flag tells the app to execute the bootstrap file\n\/\/ the routes flag is specific to my use case, this plugin is for external API only, so no need for any routes<\/code><\/pre>\n<\/li>\n<\/ol>\n<blockquote>\n<p><strong>Note<\/strong>: be careful with configuration settings because plugin config files will override matching app settings, it is better to name settings with a plugin prefix<\/p>\n<\/blockquote>\n<p>Finally, when you need to read the configuration, call <code>\\Cake\\Core\\Configure::read(&#039;ExampleAPI&#039;);<\/code> where <code>ExampleAPI<\/code> matches the config group. If you're trying to read the config inside <code>plugins\/ExamplePlugin\/src\/Plugin.php<\/code>, make sure you do it after the <code>parent::bootstrap($app)<\/code> call.<\/p>\n<p>Sources:<\/p>\n<ul>\n<li><a href=\"https:\/\/discourse.cakephp.org\/t\/best-way-to-load-plugin-configurations\/2041\/2\">https:\/\/discourse.cakephp.org\/t\/best-way-to-load-plugin-configurations\/2041\/2<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/13608613\/cakephp-plugins-best-way-to-implement-a-config-file-for-the-plugin\">https:\/\/stackoverflow.com\/questions\/13608613\/cakephp-plugins-best-way-to-implement-a-config-file-for-the-plugin<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Making a CakePHP plugin have it&#8217;s own config file<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1047],"tags":[745,715,1050],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-cakephp","tag-api","tag-cakephp","tag-cakephp-plugin-development","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1097"}],"version-history":[{"count":3,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1097\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}