tags:

views:

111

answers:

4

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

+6  A: 

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).

halfdan
+1 deleted my answer in favor of yours since yours was a few seconds first and my answer didnt add any significantly new.
Gordon
A: 

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.

stereofrog
It's not abnormal. An abnormal termination would be a fatal error or uncatched exception. (I just found out that you can do exit("Invalid data") - http://php.net/exit to terminate the script + print an error message. I'm scared now.)
halfdan
-1 for abnormal and insinuating cleanup could not be done when using exit. Exit terminates the script with Shutdown functions and object destructors.
Gordon
@halfdan: exit is abnormal is sense that it breaks normal flow of your application, what is especially important in oop/framework context of the question. Imagine a sequence of methods like `init()->run()->cleanup()` and a derived class that overrides `cleanup` for its needs. If `run()` abruptly exits, this overridden behavior will be ignored.
stereofrog
@stereofrog if the application needs `cleanup()`, you can do `register_shutdown_function(array($obj, 'cleanup')` so that's not an argument.
Gordon
A: 

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.

Frankie
Why use exit() on AJAX calls? He should rather return a JSON string containing a good error message to be displayed on the client side.
halfdan
use exit on ajax calls for when nefarious types are calling you ajax page in a non ajax way, same as you would a normal page where you should halt execution after an error.
Matt Ellen
@halfdan my thoughts on exit() on AJAX run similar to those of Matt. Besides, as the user was asking what was more efficient within Cake, halt-stopping and moving along is more efficient than letting Cake finish gracefully.
Frankie
+3  A: 

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.

deceze