Question

I'm trying to use GET|POST|PUT|DELETE in my controllers.

I feel like my routes.php file is going to get messy as my project bigger and bigger, as I will have to register lots of routes for all the GET|POST... requests.

This is the structure I'm using now:

<?php
/*
|--------------------------------------------------------------------------
| Home Routes
|--------------------------------------------------------------------------
*/

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

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

/*
|--------------------------------------------------------------------------
| Appointment Routes
|--------------------------------------------------------------------------
*/
//Get
Route::get('appointments', 'ApptController@getIndex');

Is there a better way to do this? How can I organize/improve this file?

Can you give me some advice? Thanks!

Was it helpful?

Solution

You can route incoming requests directly to your existing Controllers using the Route::controller() method in routes.php:

Route::controller('home', 'HomeController');
Route::controller('appointments', 'AppointmentsController');

Then all requests to home/* will go be handled by your HomeController, appointments/* will go to AppointmentsController etc etc etc.

It looks you have already created your functions in the Controller using the HTTP method, so then:

a HTTP GET to '/' will be handled by HomeController@getIndex()
a HTTP GET to 'logout' will be handled by HomeController@getLogout()
a HTTP POST to 'login' will be handled by HomeController@postLogin()

More in the excellent and eloquent Laravel docs here

OTHER TIPS

There some ways you can improove your routes, in the case above, you can for example do this:

Route::group(array('prefix' => 'home'), function()
{
    Route::get('logout', 'HomeController@getLogout');
    Route::post('login', 'HomeController@postLogin');
});

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

Another way to improove you code, is think more restful. This Way you can handles methods like create, delete, index, update in one line of code like so:

Route::resource('products', 'ProductsController');

Take a look at: http://laravel.com/docs/controllers#resource-controllers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top