Last active
August 29, 2015 14:20
-
-
Save chadtomkiss/25bddf438824808345ce to your computer and use it in GitHub Desktop.
Organizing Laravel Routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Some tips on how best to organize your routes | |
#1 Routes should help you determine what URLs are available in your app, | |
so don't use Route::controller or Route::resource | |
because nobody will have a fucking clue. | |
#2 Use named routes that are dot annotated so that URLs can change without breaking your Forms/Redirects etc. | |
Form::open(['route' => 'dashboard.login.post']); | |
Redirect::route('dashboard.campaigns'); | |
#3 If you find yourself repeating the first part of the URL, stick them in a Group | |
You can namespace them too if you want. | |
dashboard/campaigns | |
dashboard/campaigns/create | |
dashboard/login | |
#4 Make Logout a POST not a link. | |
#5 If you are using Laravel 5, then you will need to change the 'before' => 'auth' to 'middleware' => 'auth' | |
*/ | |
// Bad Routes | |
Route::controller('users', 'UserController'); | |
Route::resource('photos', 'PhotoController'); | |
// Bad named route | |
Route::get('campaigns', ['as' => 'view_all_campaigns', 'uses' => 'Dashboard\CampaignController@getIndex']); | |
// Good Routes <3 | |
Route::group(['prefix' => 'dashboard'], function() { | |
Route::group(['before' => 'auth'], function() { | |
// Dashboard Overview | |
Route::get('/', ['as' => 'dashboard', 'uses' => 'Dashboard\HomeController@getIndex']); | |
// View all Campaigns | |
Route::get('campaigns', ['as' => 'dashboard.campaigns', 'uses' => 'Dashboard\CampaignController@getIndex']); | |
// Create a Campaign | |
Route::get('campaigns/create', ['as' => 'dashboard.campaigns.create', 'uses' => 'Dashboard\CampaignController@getCreate']); | |
Route::post('campaigns', ['as' => 'dashboard.campaigns.create.post', 'uses' => 'Dashboard\CampaignController@postCreate']); | |
// Update a Campaign | |
Route::get('campaigns/{id}', ['as' => 'dashboard.campaigns.update', 'uses' => 'Dashboard\CampaignController@getUpdate']); | |
Route::put('campaigns/{id}', ['as' => 'dashboard.campaigns.update.post', 'uses' => 'Dashboard\CampaignController@postUpdate']); | |
// Delete a Campaign | |
Route::delete('campaigns/{id}', ['as' => 'dashboard.campaigns.delete', 'uses' => 'Dashboard\CampaignController@delete']); | |
}); | |
// Non Authed Dashboard routes can go here if applicable | |
}); | |
Route::get('login', ['as' => 'auth.login', 'uses' => 'AuthController@getLogin']); | |
Route::post('login', ['as' => 'auth.login.post', 'uses' => 'AuthController@postLogin']); | |
Route::post('logout', ['as' => 'auth.logout', 'before' => 'auth', 'uses' => 'AuthController@postLogout']); |
I don't know if you are using L4 but in L5 to leverage the default authentication I think you need to change 'before'
to 'middleware'
Updated the intro. Thanks man :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice!