Welcome Guest, Not a member yet? Register   Sign In
Sending user messages
#1

[eluser]iambaz[/eluser]
Hi,

I am redeveloping a CMS from my own framework into the codeigniter framework. At lots of points throughout the CMS, I need to send the user status messages, for example, when they add a new page, I need to tell the user 'Page added successfully' and other messages like this.

Generally these messages need to be displayed after a redirect, so for example if a new page form is submitted, the controller will be called, it will tell the model to save the data and then it will redirect the user to the previous page where a status message should be displayed.

I used to do it simply by sending it in the redirect url, for example

Code:
$model->save_data();
header("Location: http://www.mysite.com/pages.php?status=Pages added successfully");

The controller would pick this up via $_GET and post it in the template.

My question is what is the best method to achieve this in code igniter? Are there any libraries that might help or will I just have to send it as a URL segment?

Many Thanks
#2

[eluser]bretticus[/eluser]
I would personally queue the message in a session (CI's session class flash_data would be perfect since flash_data only serializes data until after the next page load.)

In addition, it wouldn't hurt to setup some constants as status codes or something to that effect. If you do end up sending the status as a URI segment, you can redirect to http://.../status/100 instead of having to send the whole string in the URL (not to mention the security risks involved in allowing text from the URL to be displayed inline.)

Finally, if you prefer to use GET variables, there is a quick hack that doesn't change anything fundamental in the framework that you can implement at the controller level. (You might have to fiddle a little with $config['uri_protocol']. I've heard setting it to 'PATH_INFO' works well for this hack. A simple print_r($_SERVER) is a good way to see which global variables contain your GET data too.)
#3

[eluser]coffeeandcode[/eluser]
[quote author="iambaz" date="1282684147"]Generally these messages need to be displayed after a redirect, so for example if a new page form is submitted, the controller will be called, it will tell the model to save the data and then it will redirect the user to the previous page where a status message should be displayed.[/quote]

Is there a reason why the redirect is necessary? In an app I'm working on, I have all actions on the controller index page, and so there is no need to redirect.

ie:
Code:
class Pages extends Controller {

    function Pages() {
        parent::Controller();
    }

    function index() {
        if ($this->input-post("add")) {
            // add add page form to view

        }
            
        if ($this->input->post("submit_add") {
                // add new post
        }
    }
}

You can break up the actions into controller methods.. prefix them with an underscore to make them private.. that way you can control what will be accessible via the url and what will not.
#4

[eluser]iambaz[/eluser]
Thanks for the replies guys,

[quote author="coffeeandcode" date="1282686707"]
Is there a reason why the redirect is necessary? In an app I'm working on, I have all actions on the controller index page, and so there is no need to redirect.
[/quote]

I have been following the general design of the former version of the CMS which was not built on such a robust framework. I think this is how I will do it when I build the next version, but at the moment I need something a bit quicker to get the thing up and running.

[quote author="bretticus" date="1282686531"]I would personally queue the message in a session (CI's session class flash_data would be perfect since flash_data only serializes data until after the next page load.)
[/quote]

This makes sense - I was always previously worried about leaving redundant data in sessions but flash_data sounds ideal. I'll give it a go.

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB