Skip to content

Laravel Routes

Laravel Routes#

URL endpoints defined at app/Http/routes.php

Using RESTful routes#

It is important to dedicate each controller to an associated resources

Simple explanation on REST

All you need to do in laravel for each controller is:

Route::resource('lists', 'ListController');

Then you would need the following actions: index, create, store, show, edit, update, destroy

Using Implicit Routes (Non REST)#

A controller isn’t intended for REST then you can just use:

Route::controllers({
    'lists' => 'ListsController'
  });

You would then need to ensure that the action names are prefixed with get or post so that laravel knows.

class ListController extends Controller {
    public function getIndex(){
      return ...
    }

    public function getCreate()...

    public function postStore()...
}

Defining Route Parameters#

Route::get('blog/category/{category}', 'BlogController@category');

So {category} would become whatever is in the url ie. /blog/category/food

$category = 'food';

You would need the action to be setup:

public function category($category){
  return view('blob.category')->with('category', $category);
}

Caveat: They need to be specified in the same order as parameters are specified

Caveat: parameters are required in the below case

Route::get('blog/category/{category}/{subcategory}', 'blogController@category');

public function category($category, $subcategory){
  return view('blog.category')
    ->with('category', $category)
    ->with('category', $subcategory);
}

To make parameters not required is quote a rigmeral if you ask me

Route::get('blog/category/{category?}', 'BlogController@category');

public function category($category = ''){
  $category = $category == '' ? 'php : $category;'
  return view('blob.category')->with('category', $category);
}

## Route Aliases / Named Routes

These allow you to not have to change all links, when there is a change

Route::get(‘blog/category/{category}’, [‘as’ => ‘blog.category’, ‘uses’ => ‘BlogController@category’]);


#### Referencing Routes