It depends. If the method is not an action, but a private method
called from within the controller (eg. $this->__whatever()) then
returning false is a good idea. It really depends on wht you want from
the method.

If it's an action (ie. called directly through the request) you're
better off setting an error msg--whether in Session->flash() or an
error variable--or redirecting (again, using flash to set a msg).

The usual way to set up an action's flow is to check if $this->data is
empty. If not, this is the 2nd request (a form has been submitted).
Generally, that check can go right at the top. But, in some cases, you
might want to do some checking whether it's the 1st or 2nd request. In
that case, do that first. If it fails, you might want to redirect.


function someAction($id = null)
{
        /* this will be run for all requests/submissions
         */
        if (!$id)
        {
                $this->Session->flash('ruh-roh!');
                
                /* processing will stop after the following
                 */
                $this->redirect('somewhere else');
        }
        
        if (!empty($this->data))
        {
                /* deal with subimtted content
                 */
                $this->Something->create($this->data);

                if ($this->Something->save())
                {
                        /* set a msg and redirect so processing stops
                         */
                        $this->Session->flash('done');
                        $this->redirect('somewhere else');
                }
                else
                {
                        /* set error msg but do not redirect so further
                         * processing can happen further down
                         */
                        $this->Session->flash('ruh-roh again!');
                }
        }
        else
        {
                /* first request, so grab some data
                 */
                $this->data = $this->Something->read(null, $id);
        }
        
        /* Grab some other data. If save failed for some reason,
         * this other data will still be set.
         */
        $this->set('foo', $this->Something->Foo->findByBar('bar'));
}

On Mon, May 18, 2009 at 4:58 PM, Brian Lee <brianleeu...@gmail.com> wrote:
>
>
> In methods of controllers, there are usually many checks for "doing
> the right thing".
> For instance, I check if user is accessing the right data.
>
> However, I'm wondering what is the best way to return or exit from
> these methods when errors do occur. I've been using just simple return
> statement, but is there anything that's more CakePHP-like and follow
> the framework's design? or is simple return/exit statement good
> enough?
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to