HiveBrain v1.2.0
Get Started
← Back to all entries
patternphplaravelMinor

Routing for a Laravel 5 e-commerce site

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sitecommerceforroutinglaravel

Problem

I am new here and learning Laravel 5 on my own. I want to get reviewed on the following routes.php code.

My question are:

  • Have I made any mistakes by doing this? If yes, then what could be the possible solution?



  • Have I followed the proper convention?



  • Do I need to optimize my code? If yes, then how do I optimize it?



```
<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/*
| ------------------------------------------------------------------------
| Admin User Section Routes
| ------------------------------------------------------------------------
|
| All the routes that are related to admin
|
*/
Route::get('admin', 'AdministratorController@index');

Route::post('admin/login', 'AdministratorController@postLogin');

Route::get('admin/logout', 'AdministratorController@getLogout');

Route::get('admin/products', 'ProductsController@index');

Route::get('admin/products/add', 'ProductsController@create');

Route::post('admin/products', 'ProductsController@store');

Route::get('admin/products/edit/{id}', 'ProductsController@edit');

Route::post('admin/products/update/{id}', 'ProductsController@update');

Route::get('admin/categories', 'CategoriesController@index');

Route::post('admin/categories/update/{id}', 'CategoriesController@update');

Route::get('admin/categories/edit/{id}', 'CategoriesController@edit');

Route::get('admin/categories/add', 'CategoriesController@create');

Route::post('admin/categories', 'CategoriesController@store');

Route::get('admin/orders', 'AdminOrdersController@index');

Route::get('admin/orders/edit/{id}', 'AdminOrdersController@edit');

Route::post('admin/orders/updateOrderStatus/{order_i

Solution

There is nothing wrong with this approach at all. Rather than say you should or shouldn't do it any other way I'm going to give you an alternative approach that allows you to write less code in your routes.php file.

I've noticed that your HomeController deals with a lot of different types of requests. This can get very hard to manage when your application grows. It's a good idea to separate out your controllers into manageable classes that deal with one area or feature of your application. The example below would ideally be in a PasswordController class for example.

Taking this chunk:

Route::get('/password/reset', 'HomeController@getPasswordReset');

Route::post('/password/email', 'HomeController@postPasswordReset');

Route::get('password/reset/{token}', 'HomeController@getResetPasswordForm');

Route::post('/password/reset', 'HomeController@postPasswordUpdate');


You can instead just register the controller

Route::controller('/password', 'HomeController');


The difference is that your routes are mapped to the controller methods.

Your GET route /password/reset would map to the method getReset in the controller. While the POST route /password/email would map to postEmail in the controller.

Also, the GET route /password would be handled by getIndex in the controller.

Changing the routes is a case of changing the controller methods. This means one place to change your routes rather than two.

Code Snippets

Route::get('/password/reset', 'HomeController@getPasswordReset');

Route::post('/password/email', 'HomeController@postPasswordReset');

Route::get('password/reset/{token}', 'HomeController@getResetPasswordForm');

Route::post('/password/reset', 'HomeController@postPasswordUpdate');
Route::controller('/password', 'HomeController');

Context

StackExchange Code Review Q#87931, answer score: 3

Revisions (0)

No revisions yet.