# Routing for a Laravel 5 e-commerce site

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:

1. Have I made any mistakes by doing this? If yes, then what could be the possible solution?
2. Have I followed the proper convention?
3. 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.
|
*/

/*
| ------------------------------------------------------------------------
| ------------------------------------------------------------------------
|
| All the routes that are related to admin
|
*/

/*
| ------------------------------------------------------------------------
| Normal User Section Routes
| ------------------------------------------------------------------------
|
| All the routes that are related to normal user
|
*/

Route::get('/', 'HomeController@index');

Route::get('/register', 'HomeController@getRegister');

Route::post('/register', 'HomeController@postRegister');

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

Route::get('/product/{id}/{name}', 'ProductsForCustomers@show');

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

Route::get('/cart', 'CartController@index');

Route::get('/cart/empty', 'CartController@emptyCart');

Route::get('/cart/{id}', 'CartController@store');

Route::get('/cart/remove/{id}', 'CartController@destroy');

/*
| ------------------------------------------------------------------------
| Checkout Section Routes
| ------------------------------------------------------------------------
|
| All the routes that are related to normal user
|
*/

Route::get('/checkout', 'CheckoutController@index');

Route::get('/checkout/register', 'CheckoutController@getRegister');

Route::post('/checkout/register', 'CheckoutController@postRegister');

Route::get('/checkout/confirm', 'CheckoutController@getConfirmCart');

/*
| ------------------------------------------------------------------------
| Order Section
| ------------------------------------------------------------------------
|
| All the routes that are related to orders
|
*/

Route::get('/order', 'OrdersController@store');

/*
| ------------------------------------------------------------------------
| Category Section
| ------------------------------------------------------------------------
|
| All the routes that are related to categories
|
*/

Route::get('/category/{name}', 'HomeController@getCategoryProducts');

Route::get('/category/{name}/{subcategoryname}', 'HomeController@getSubCategoryProducts');

/*
| ------------------------------------------------------------------------
| ------------------------------------------------------------------------
|
| All the routes that are related to normal user's password resetting
|
*/

/*
| ------------------------------------------------------------------------
| Tags Section
| ------------------------------------------------------------------------
|
| All the routes that are related to products tagged
|
*/

Route::get('/products/tagged/{name}', 'TagsController@getProducts');

/*
| ------------------------------------------------------------------------
| Users My Account Section
| ------------------------------------------------------------------------
|
| All the routes that are related to normal user's My Account
|
*/



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');



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.

• So you are asking me to create a new controller called PasswordController and in routes.php file, I will have to replace the password chunk of code with newly created PasswordController.. Right ? – Saiyan Prince Apr 26 '15 at 12:24

I do not know if your admin controllers are in the separate folder, but I see that they have the same prefix - admin. Laravel has such route groups.

So you can just use

Route::group(['prefix' => 'admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});


You can also use nested groups.

And it is not required to start you route with /. You can omit this.