Routes are located in the "routes/web.php" file. They are assigned the "web" middleware group. The first parameter of the get
method is where the request is sent from and the second parameter is where the request is routed to.
Basic Get Route:
Route::get('foo', function () {
return 'Hello World';
});
In this case, the parameter that decides where the request is routed to, is instead presented a function that returns “Hello World” to the screen. Going to example.com/foo will show a blank page with “Hello World” in it.
You can also use a route to reference a controller:
Route::get('/post/{id}', 'PostsController@show');
The Post, Put and Patch routes are used for setting/submitting data between views. In the CRUD model, these routes cover storing and updating.
Example:
Route::post('foo/store', 'Controller@store');
Route::patch('foo/update', 'Controller@update');
In terms of Laravel. It is generally assumed to use POST for creating/storing data and PATCH for editing/updating data. PATCH will allow the automatic updating of TIMESTAMPS.
The Delete routes are simply used to remove data.
Example:
Route::delete('foo/delete', 'Controller@destroy');
Route::get('posts', 'PostController@index')
->name('posts.index');
Route::get('posts/{id}', 'PostController@show')
->name('posts.show');
Route::get('posts/create', 'PostController@create')
->name('posts.create');
Route::post('posts/{id}/store', 'PostController@store')
->name('posts.store');
Route::get('posts/{id}/edit', 'PostController@edit')
->name('posts.edit');
Route::patch('posts/{id}/update', 'PostController@update')
->name('posts.update');
Route::delete('posts/{id}/delete', 'PostController@delete')
->name('posts.delete');
In this example, name is a method for the Route class that allows you to call the route by a custom name. So if you were to create a link for the view associated with the show method for posts, you would use the following:
<a href="{{ route('posts.show', ['id' => 1]) }}">
...
</a>
Resource Routes are routes associated with a resource controller. It is a shortcut to create routes for the entire CRUD model in one call.
Example:
Route::resource('posts', 'PostController');
This one call writes EVERYTHING written in the Full Crud Example shown above (including the same names). The benefit of writing everything manually is if you wish to customize the routes. The resource routes create your routing exactly like the CRUD example, but if you wanted to change the URI, the method of the controller to call, or the name of the route, you need to write the statements manually.
Redirect Routes are routes used for the sole purpose of redirecting to another page.
Example:
Route::redirect('/here', '/there', 301);
View Routes are used to return a view (as part of the framework). This is a shortcut from defining a full route through a controller.
Route::view('/welcome', 'welcome');