ppazos / yupp

Automatically exported from code.google.com/p/yupp
0 stars 0 forks source link

Agregar metodo getCheck en YuppController para encapsular logica de get(id) #184

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
El código seguro para get por un param id es:

// 1. preguntar si viene id
if (!isset($this->params['id']))
{
    header('Content-type: application/json');
    return $this->renderString( json_encode(array('message'=>'id is required', 'status'=>'error', 'errors'=>array())) );
}

// 2. hacer el get  
$ins = TheClass::get( $this->params['id'] );

// 3. ver si se obtuvo una instancia
if ($ins == null)
{
    header('Content-type: application/json');
    return $this->renderString( json_encode(array('message'=>'the instance doesnt exists', 'status'=>'error', 'errors'=>array())) );
}

Esto se debe hacer cada vez que se quiere hacer get(id). getCheck haría esta 
lógica y devolvería esos  2 errores mediante códigos estándar. El 
controller luego puede decidir si hacer render de una view, redirect o devolver 
un JSON (como en este caso).

Original issue reported on code.google.com by pablo.swp@gmail.com on 3 Sep 2013 at 3:39