6
\$\begingroup\$

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.
|
*/


/*
 | ------------------------------------------------------------------------
 | 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_id}/{product_id}', 'AdminOrdersController@updateOrderStatus');




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

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

Route::get('/login', 'HomeController@getLogin');

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

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

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

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::post('/checkout/login', 'CheckoutController@postLogin');

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

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

Route::post('/checkout/addShippingAddress', 'CheckoutController@postAddShippingAddress');

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







/*
 | ------------------------------------------------------------------------
 | Resetting the password
 | ------------------------------------------------------------------------
 |
 | All the routes that are related to normal user's password resetting
 |
 */
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');





/*
 | ------------------------------------------------------------------------
 | 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
 |
 */

Route::get('/{username}', 'CustomerController@index');

Route::get('/{username}/orders', 'CustomerController@getAllOrders');

Route::get('/{username}/orders/{code}', 'CustomerController@getOrder');
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Saiyan Prince Apr 26 '15 at 12:24
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.