Hi all!
I have an app which handle multiples entities and each entity (table) has an order column so user can order things with drag & drop.
To avoid duplicate methods in each controller, I have made a global method in the Base Controller named updateOrder. I use a javascript method to post datas
Is it a good practice to create a global method in the base controller ? What do you think ?
Thanks
protected function updateOrder(){
// Set the post vars
$type = Input::get('type'); // get the table name for update
parse_str(Input::get('order'),$order); // get the order string serialize by sortable function
$i = 1;
foreach($order[$type] as $order){
DB::update("UPDATE `$type` SET `order` = ? WHERE `id` = ?",[$i,$order]);
$i++;
}
return Response::json(array(
'success' => true,
'message' => 'Mise à jour de l\'ordre des éléments réussie',
200
));
}
BaseController seems a good place. I would also suggest looking for design patterns, creating basic APIs or traits if you use PHP 5.4.
May I ask why this need's to be in every controller? If you had like
protected $table = 'blablabla';
it would make sense (but would be misplaced) but since you post the type anyway, wouldn't it just be simpler to have 1 url/method for that instead?
This doesn't seem very safe. Your injecting that variable straight in the query, instead of binding parameters. You can create a BaseController with this function, use Trait with this method or use Eloquent Models and add a trait there, and just call that method in all your controllers.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community