In this post we will learn to implement the Laravel 8 routing and how it’s different from Laravel other versions. Laravel routing is used to create the URL for each POST, GET requests and it works as a bridge between the controller or html view.
In this article we will also cover to use the middleware and groups in routes. In Laravel there is 4 default routes files.
- web.php
- api.php
- console.php
- channel.php
web.php
is used to create the request URL for web application. api.php
is used for to create the routes for rest api. we will start the Laravel routing tutorial by step by step guide.
Create the routes
In this step we will create a simple routes with a callback to return the response..
<?php
use Illuminate\Support\Facades\Route;
Route::get('/simple-view', function () {
//logic of your route
return "this is a simple response with text";
});
You can access the route using
http://domain.com/simple-view
Router methods
Laravel have 8 routes methods available to register the routes.
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
// match for allow multiple request method
Route::match(['get', 'post'], '/', function () {
//
});
// any for any type of request method
Route::any('/', function () {
//
});
Call view using the route
We can also call directly the view file using the Laravel 8 route.
Route::view('/view-file', 'index');
//or
Route::get("/view-file",function(){
return view("index");
})
and view file
<p>this is a view file</p>
You can access the route using
http://domain.com/view-file
Creating redirect routes in Laravel 8
In Laravel 8 we can create redirect routes in route file
Route::redirect('/path-here', '/to-path', 301);
Creating Named Routes in larevel 8
Route::get('/home', function(){
})->name("home");
Calling controller method in Laravel routes
<?php
use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;
Route::get('/create-post',[PostController::class, 'create']);
Parameterized routes
we can pass the parameter to url and can access in our controller
<?php
use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;
Route::get('/edit-post/{id}',[PostController::class, 'editPost']);
and th controller file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Show the form to create a new blog post.
*
* @return \Illuminate\View\View
*/
public function editPost(Request $request,$id)
{
return view('post.edit',["id"=>$id]);
}
}
Create Namespace and group routing in Laravel 8
By using the namespace and group we can apply multiple rules to multiple routes at once check the below code
Route::namespace('\App\Http\Controllers\Api')->group(function(){
Route::get("app/test",'PostController@create');
});
Now, create a folder Api
in App\Http\Controllers
and paste the below code
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Show the form to create a new blog post.
*
* @return \Illuminate\View\View
*/
public function create()
{
return "create view";
}
}