Support the ongoing development of Laravel.io →
posted 11 years ago
Architecture

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
    ));
}
Last updated 3 years ago.
0

Any answer ?

Thanks

Last updated 3 years ago.
0

BaseController seems a good place. I would also suggest looking for design patterns, creating basic APIs or traits if you use PHP 5.4.

Last updated 3 years ago.
0

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?

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

bidibule bidibule Joined 4 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.