3

i'm realizing an admin panel and i need to toggle an option on/off. Everything works fine, except for the Configure::write() method that looks like if it's not permanent. Here is the ajax handler.

        case ("toggle_button"):
        if($_POST['status']=="On"){

            Configure::write('tag_system',0);


            die("Off");

        }
        elseif($_POST['status']=="Off"){
            Configure::write('tag_system',1);

            die("On");

        }


        break;

If i try

die(Configure::read('tag_system'));

it contains the correct value but when i reload the page, the value is missing. It's not set in the general config file but when i did, the behavious was similar but instead of a blank value, Configure::read returned the value in the config file.

How should i handle this?

2 Answers 2

1

The Configure class writes values you pass to it into memory which are then only available during that request.

If you need to use the value in subsequent requests, which it sounds like you do, then you need to write the value to session.

# write
$this->Session->write('tag_system', 1);

# read
$this->Session->read('tag_system');
Sign up to request clarification or add additional context in comments.

4 Comments

I need it to be permanent for the whole system, not just for the user. Isn't there a way to do it with cakephp cacher or stuff like that? I don't really know the standard to store these kind of global settings.
That's exactly what i would like to avoid. I'm working on a cms and i'm trying to develop it as clean as possible. To solve this i should create a table "temp_settings" and store a single value there? I know it's easy but i don't know if it's a good practice. If there's no other way, i will do this way.
It's perfectly fine to do that. It's referred to as key-value pair storage and you could use it to store various site-wide settings that could potentially be changed by an admin.
Well the big problem is that this cms has been developed for 3 years by lot of people but noone ever felt the necessity to do this. That's why i'm not really sure about doing this.
1

I've had an issue with this before and it was because I misunderstood the purpose of the Configure component. It wasn't meant to 'last' between refreshes. Everything in your config file is readable, and temporarily 'changeable' on execution of a script. After execution, nothing is saved.

I believe the benefit is being able to use it in between models, controllers, and views.

Key statement is here: "CakePHP’s new Configure class can be used to store and retrieve application or runtime specific values."

http://book.cakephp.org/view/924/The-Configuration-Class

CakePHP has many storage solutions:

Session: http://book.cakephp.org/view/173/Sessions (component) http://book.cakephp.org/view/484/Session (helper)

Cache: http://book.cakephp.org/view/1376/Cache (helper) http://book.cakephp.org/view/1511/Cache (utility)

There's also ACL if it's applicable to you. CakePHP supports acl by ini files amongst other things.

Probably the most popular way of setting global configuration, is through the database. Once you retrieve this data though, you can cache it or set it into the session using the methods above. This way you don't have to repeatedly query the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.