dimanche 27 novembre 2016

Organization of controller logic in Laravel

Suppose a customer can log in to their account where they can view their orders, manage payments, change security settings, etc. Admins can do the same, but their front-end (i.e. views) will be different from that of customers, because they need privileged access and greater control of data.

Now, with RESTful design, Laravel recommends to split routes by entities that they represent:

// Orders
Router::get('orders', 'OrderController@index');
Router::get('orders/{order}', 'OrderController@show');
// Payments
Router::get('payments', 'PaymentController@index');
Router::get('payments/{payment}', 'PaymentController@show');
// etc.

If I follow this pattern, then

  • each controller method (e.g. OrderController@index, PaymentController@show) will have an if-else construct -- one to handle the logic when Auth::user() is a customer, and the other -- when an admin; this will result in code redundancy
  • some methods may never be used by customers (e.g. delete or put), while they are likely to be needed for admins; this will just add confusion to code

An alternative that I can think of is to have something like this:

// Customer routes
Router::get('account/orders', 'AccountController@index_orders');
Router::get('account/payments', 'AccountController@index_payments');
// Admin routes
Router::get('admin/orders', 'AdminController@index_orders');
Router::get('admin/payments', 'AdminController@index_payments');
// etc. etc.

The problem with this approach is that controllers will get convoluted and large with all the different methods for various entities (e.g. Order, Payment, etc.).

Lastly, I can think of the followind 3rd option:

// Customer routes
Router::get('orders', 'OrderController@index');
Router::get('payments', 'PaymentController@index');
// Admin routes
Router::get('a/orders', 'Admins/OrderController@index');
Router::get('a/payments', 'Admins/PaymentController@index');
// etc. etc.

/* dir structure: */
/app
    /Http
        /Controllers
              /Admins
                   OrderController.php         --  admins only 
                   PaymentController.php       --  admins only 
              OrderController.php     -- customers
              PaymentController.php   -- customers

This complies with the RESTful design and separates admins into their own route group (with admin/ prefix before each route). The problem, clearly, is that some controller logic will be duplicated.

What is the best approach to dealing with this? Any other ways to do it? Thank you!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2fCzkBi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire