Welcome Guest, Not a member yet? Register   Sign In
Which message library to use?
#1

[eluser]victorche[/eluser]
Hello to @all! I am in a need of a global solution for my site. It is about displaying all kinds of messages/errors.
I need a simple and nice solution and I came across 2 different libraries. The second one is written by @n0xie and is based on the first one.
Here are the links:
http://github.com/jeroenvdgulik/codeigniter-message/ - Written by n0xie
http://codeigniter.com/wiki/Message/ - The original one, current version 2.1

My questions are... Which one is better, more flexible? I am using CI 2.0 and PHP5, so which one is ready for CI 2.0?

Please, people who are using one of those... Give me a hint
#2

[eluser]n0xie[/eluser]
Obviously I'm biased but I'll humour you. ;-)

Depending on what you want my library can style individual messages via view partials. So for instance it will wrap all the 'notice' messages into a 'notice' div, all the 'error' messages into an error div and all the 'success' messages into a 'success' div. This is all done automatically by convention.

See the example controller in my repository.

I haven't tested my library extensively on CI2.0 but I suspect no difference in functionality.
#3

[eluser]victorche[/eluser]
Thank you, @n0xie! Will give it a try... My question was how can I use it together with flashdata messages, I mean something as a small example.
:]
#4

[eluser]n0xie[/eluser]
Take a look at the example controller...
#5

[eluser]victorche[/eluser]
@n0xie, thanks! But anyway I can not solve this. First question is:
What is the difference between
Code:
$this->message->display();
$this->message->set();
$this->message-get();
And one more... For example see the ion_auth messages in their example controller:
Code:
// First setting the message after a login submit:
$this->session->set_flashdata('message', $this->ion_auth->messages());

// Then if we have a message, display it:
$data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

// And pass the message to the $message var in the view file:
$this->load->view('auth/index', $data);
With the above example, I have to put a hardcoded div in the view, which is populated by text (error, message) if there is one. I don't want it this way, that's why I need your library. I don't want something like this in my view:
Code:
<div id="message">&lt;?php echo $message; ?&gt;</div>
The reason is ... Impossible styling. Why? Because this div is always in the html code. If there is no style, it is Ok. Because with the help of reset.css this div is not visible without a text (message) in it. But if I want to make it look nice, then it is always visible. I mean if I add some css about it...
So I tried to modify this code example like:
Code:
// Then if we have a message, display it:
$message = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

// Let's use the message magic:
$data['message'] = $this->message->set($message);

// And pass the message to the $message var in the view file:
$this->load->view('auth/index', $data);
It is not working, so maybe I am making a big mistake... Any hints?
#6

[eluser]n0xie[/eluser]
Did you at least take a look at the example controller?

Code:
// Then if we have a message, display it:
$message = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

// Set message
$this->message->set('error', $message);

// load view
$this->load->view('auth/index');

// in your view:
echo $this->message->display();
As mentioned before you can style the output using a view partial. Add a view to the /view/messages/ folder with the same name as your message 'group'. For example your errors can be styled using error_view.php, as shown in the example.
#7

[eluser]victorche[/eluser]
Thank you, @n0xie! I was doing almost the same, but...
Code:
$this->session->flashdata('message'); // Sometimes here i have both errors and messages ...
In this case it is hard to send the message to the correct msg group. I have templates for 'notice' and 'error', but It may return both.
So I am doing like:
Code:
$this->message->set($message); // Without specifying the msg type. And this way it is using the default wrapper from the config.
Anyway I will think of a smarter way to do it, thanks again :]
#8

[eluser]victorche[/eluser]
By the way, there is a small error, I think... After line 163:
Code:
// does a default view partial exist?
            elseif (file_exists(APPPATH.'views/'.$this->message_folder.$this->message_view.'_view'.EXT))
            {
                $output .= $this->CI->load->view($this->message_folder.$this->message_view, array('messages'=>$messages), TRUE);
            }
Should be:
Code:
// does a default view partial exist?
            elseif (file_exists(APPPATH.'views/'.$this->message_folder.$this->message_view.'_view'.EXT))
            {
                $output .= $this->CI->load->view($this->message_folder.$this->message_view.'_view', array('messages'=>$messages), TRUE);
            }
@n0xie, my last question is like this... For example, with form validation, I can have more than one error. Like:
Quote:The email field is required.
The password field is required.
In this case I have to do like:
Code:
$message[] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
As $message in this case is an array. If i don't do it, the message div is displayed correctly, but only the last error is displayed. But if I do it like an array, I get all the errors, but the message div is always displayed Sad
Maybe because ... Even an empty array makes the div visible? I think (I am a newbie in programming) there should be a check if the array is empty, or ... what?

Can you give me one last hint about this? Thanks in advance!
#9

[eluser]n0xie[/eluser]
For the first part of the question: it's not necessary in CodeIgniter to add the _view part when loading a view. The loader will look for the file with or without the _view part.

For the second part I think I found your bug. The validation_errors() array returns 'something' even if there are no errors yet. I updated the Message Library to skip empty messages. Thanks for the bug report!
#10

[eluser]victorche[/eluser]
[quote author="n0xie" date="1286285999"]For the first part of the question: it's not necessary in CodeIgniter to add the _view part when loading a view. The loader will look for the file with or without the _view part.

For the second part I think I found your bug. The validation_errors() array returns 'something' even if there are no errors yet. I updated the Message Library to skip empty messages. Thanks for the bug report![/quote]
Thanks, n0xie!
Anyway... The first part, my config looks like this:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['message_prefix']    = ''; // blank, as auth library already ouputs msgs with <p> tag
$config['message_suffix']    = ''; // blank, the same as the prefix
$config['message_folder']    = ''; // blank, our message_view.php is located in the main /views forder
$config['message_view']        = 'message'; // the library is making a check with .'_view', thats why i am adding only 'message' here
$config['wrapper_prefix']    = ''; // blank, we will use a view instead
$config['wrapper_suffix']    = ''; // blank, the same as prefix
I have a file message_view.php in /views folder. And the result is:
Quote:Error
File views/message.php can not be found.
If I make the change (as showed 2 posts above) it works perfectly. Do you mean that this check:
Code:
elseif (file_exists(APPPATH.'views/'.$this->message_folder.$this->message_view.'_view'.EXT))
Will pass, if the name of the file is message_view.php?
I think this way, it will look for message_view_view.php, if in the config I am using:
Code:
$config['message_view']        = 'message_view'; // the library is making a check with .'_view', so i think this will be a problem?




Theme © iAndrew 2016 - Forum software by © MyBB