Kohana success/error messages method - php

I am slightly confused as to what the best method is of handling redirecting and displaying error/ success messages using an MVC framework, specifically Kohana.
I have a Controller User which extends the Base controller.
Am am trying to use the action_remove() function in the base controller then redirect back to the page they were on and display a message 'User has been removed....'
I don't want to pass the error message in the GET params. Is there a standard way of doing this?

You should try to use flash session data. It is very useful when You want to show errors as well as messages. At first access flash data is removed so it can be accessed only once.
http://docs.kohanaphp.com/libraries/session#flash_session_data
Also there was some related post about this here Which is the best way to display 'flash messages' in kohana v3?

You can use Message Modules in kohana 3.x. its used to display messages.
please download this module it from here and extract . Then paste it in modules folder.
https://github.com/GoldCoastMedia/kohana-flash
Then enable it in applications/bootstrap.php like as follows.
'message' => MODPATH.'message',
There are 5 type of messages are available. success, error, warning, info, notice. You can give styles for each messages . but you need to write class in the same name of message type.
Message::error('pls login to access');
//to assign message type.its error message.
echo Message::display();
//to display it
thats it. but remember that you need to create class in the name of success, error, warning, info, notice to apply styles.
to check condition in view file , you can use it.
$sucessful_message=Message::display();
if($sucessful_message) { ?>
<div id="messagedisplay" class="padding_150">
<div class="notice_message">
<?php echo $sucessful_message; ?>
</div>
</div>
<?php } ?>

Related

how I use flash message in yii2?

how I add flash message to my site using related controller please explain step by step i'm new to the php.
I use yii2 framework to build the site and i need to print flash message in the index page using create controller.
Currently the question is too broad, but here is basic usage:
1) You can set in controller like that:
\Yii::$app->session->setFlash('flashMessage', 'Hello world!');
2) Then you can display it in view like so:
echo \Yii::$app->session->getFlash('flashMessage');
Optionally you can check the existence with:
\Yii::$app->session->hasFlash('flashMessage');
Official docs:
Session
setFlash()
hasFlash()
getFlash()
There are actually more methods for working with flashes, you can see it in official docs.
Also advanced template provides useful widget Alert that integrated with Boostrap 3:
\Yii::$app->session->setFlash('error', 'This is the error message');
...
echo Alert::widget();
For better understanding of working with flash messages, go through # http://www.yiiframework.com. By following this method, you can done printing flash messages in your web page.

How to use editUrl attribute of jQgrid in jQuery

I have been trying to implement edit/add of rows in jqgrid dynamically but stuck at place with editUrl attribute. Can somebody tell me that
1) Where to keep this php file, in server or in our applcation?
2) I am using like editurl: './data_save.php', and I have this file in my application, next to my jsp file. But on submit, I am getting 'error Status: 'Not Found'. Error code: 404' on top of popup window and on console I am getting, no mapping found for request with URI [/myapp/some.php] in distacherservlet with name 'myApp'.
Any idea?
Update :-
I also tried using, editurl:'/some.jsp' and when I create on controller(expecting this url) in my Spring MVC Controller class then I do not get message at console saying, "no mapping found for request with URI [/myapp/some.php] in distacherservlet with name 'myApp'"
Use your application name
editUrl:'WhatEverYourApplicationNameIs/data_save.php'
Update1:
editUrl:'#Url.Action("ActionName","ControllerName")'
Update2:
If your edit method is like below
public .. MyEditMethod()
{
//Add logic
}
then your editUrl will be
editUrl:'#Url.Action("MyEditMethod","WhatEverYourControllecNameIs")'

Passing multiple parameters to index controller using CodeIgniter-like MVC

I am just beginning to learn PHP, so I apologize if this is a basic question. I am using what I understand to be a CodeIgniter-like MVC framework (NOT CI though - a homegrown framework)
I am trying to pass two parameters to my index controller, each of which will display a different error message. The errors are generated from two individual post functions (i.e., if user's log in is incorrect and if email already exists at sign up).
public function index($error1=NULL, $error2=NULL) {
$this->template->content = View::instance('v_index_index');
$this->template->content->error1 = $error1;
$this->template->content->error2 = $error2;
echo $this->template;
}
What I am observing is that the only error displayed is the parameter that appears in the parentheses first (e.g., at index/index/error2 the error1 message is displayed). I've already tested the logic for determining the error type, so I know that is correct and believe it must have something to do with the above.
Any help is greatly appreciated!

Manipulate $session->flash() view output in CakePHP

I am looking to manipulate the $session->flash() output in my CakePHP app, Currently I have the very simple default implementation of showing flash and auth error messages:
<?php
$session->flash();
$session->flash('auth');
?>
This produces a <div> with an ID and class that has the message inside. What I would like to do is wrap/replace the generated HTML, specifically with some jQuery UI classes, but wrapping is difficult as I am unable to tell when there is actually a message going to be displayed so I end up with an empty but style error div. What I really need for wrapping to work is to check in $session->flash() returns anything, but I get 'can't use method return value in write context' when checking it with empty();
As far as I can tell the generated HTML is hard coded into the session helper! Bonus points if you can work out how to change the class on the auth message and normal flash message independently.
To check if a message is going to be flashed, put this in the layout
<?php if($session->check('Message')){ echo $this->Session->flash();} ?>
CSS attributs can be set when you set the message to be flashed
http://book.cakephp.org/view/1311/Methods#setFlash-1313
read up on setFlash() and use the other params that the method takes to define your own elements. you can then do what ever you like.
http://book.cakephp.org/view/400/setFlash
A work-around solution I have found is to set $session->flash() to a variable and then check it with empty() so I can echo out the appropriate <div> if necessary.
$flashMessage = $this->flash();
$authMessage = $this->flash('auth');
.. and then check if each one is empty. Of course I have some unnecessary html inside there but as far as the auth message goes, I think this is as flexible as I can get.

PHP using exit()

I am using Cakephp but this is a MVC/php doubt
letting the view display the message
vs
echo 'Invalid Data'; exit;
I would like to know is there any pitfalls in the second case like memory leak etc.. Which one is better
EDIT
In case of a ajax call is exit good. and what about memory leak and other issues . Are all variables deallocated
You should use a custom ExceptionHandler (set_error_handler / set_exception_handler) and throw an Exception if you encounter any errors (CakePHP should already provide an ExceptionHandler). Make some space in your view and if the ExceptionHandler/ErrorHandler has a message, show it there to let the user know.
Your second code will just produce a blank page containing the little text. Every user will appreciate if you show the message inside your usual page layout instead of producing a blank page (which looks broken to most people).
The Cake tools to signal errors to the user are session messages and error views.
For "passive" actions like view actions, you should throw a 404 or similar, possibly more specialized error, e.g. if the requested model does not exist:
function view($id) {
$data = $this->Model->read(null, $id);
if (!$data) {
$this->cakeError('error404');
}
...
}
See Error Handling with CakePHP.
For any POST action, you should return the user to the view and display an error message using $this->Session->setFlash('Error!') and appropriate error messages for each invalid form field. That's the default behavior of baked views and controllers.
Terminating the whole script with exit makes for a miserable user experience.
In general, you should avoid exit. Exit is an abnormal termination, and programs should not terminate abnormally. Even if an error occurs, there are still many things that needs to be done - cleanup, logging, notifying the user etc. After all, your operating system doesn't reboot every time it cannot open a file.
performance-wise (AJAX cals)
Use exit().
user experience-wise (standard site nav)
Show the error in a proper formated page keeping the user within your site.

Categories