Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

When building a plugin, how does one define user editable config files? Would it be better to create a settings UI in the plugin and have the user edit those variables in the CP? Or is there a way to do it properly with a config file?

The reason I want to opt for a config option is so the value can be localized for the specific environment to set different options. I need to add the ability to define a public directory that will be used to store generated images and obviously that path could be different on local and production environments.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You can add a config.php file in the root of your plugin's folder (craft/plugins/pluginhandle/config.php) that specifies the default values of your config items by returning an array of key value pairs for config settings and values.

If a user wants to change any of those settings, they can add a pluginhandle.php config file to craft/config/pluginhandle.php that returns an array with the overridden key value pairs.

From your plugin, if you call:

craft()->config->get('key', 'pluginhandle')

then Craft will return the user supplied value, if it exists, and fallback on the plugin supplied default value, if it does not.

Also worth nothing that all of Craft's multi-environment config stuff will work in this context for the plugin, too.

share|improve this answer
    
That's really helpful to know! –  Alec Aug 18 at 19:29

I would use plugin settings to store these kinds of values. Then in your plugin files (controller, services etc.) you could call up the settings and adjust your output as needed.

craft()->plugins->getPlugin('myPlugin')->getSettings()

As for asset paths and your site path, you can keep all of those relative by using the URL helper functions like {{ url('assets/build/css/main.css') }} and UrlHelper::getUrl('/') depending on where you are working.

Here's a related post - How do I reference the settings of my plugin?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.