In this tutorial we will see how to create new route in laravel 8. You can easily create post route in laravel 8 application.

Follow this step by step process of how to create first route in laravel 8



Create Simple Route:

routes/web.php

Route::get('simple-route', function () {
    return 'This is Simple Route Example of rathorji.in';
});

Access URL:

http://localhost:8000/simple-route



Route with Call View File:


We can create route with directly call view blade file from route directly by using Route::view method.

routes/web.php

Route::view('/blog', 'index');

Route::view('/blog', 'index', ['name' => 'nicesnippets']);

resources/views/index.php

<h1>his is Simple Route with Call View File Example of nicesnippets.com</h1>


Access URL:

http://localhost:8000/blog



Route with Controller Method:


Here, we can create route with call controller method

routes/web.php

use App\Http\Controllers\PostController;
// PostController
Route::get('/post', [PostController::class, 'index']);

app/Http/Controllers/PostController.php

<?php
  
namespace App\Http\Controllers;
  
class PostController extends Controller
{
  
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('index');
    }
}


resources/views/index.php

<h1>his is Simple Route with Controller Method Example of nicesnippets.com</h1>

Access URL:

http://localhost:8000/post




Create Route with Parameter:


Here, we will create simple route with passing parameters.

routes/web.php

use App\Http\Controllers\PostController;
// PostController
Route::get('/post/{id}', [PostController::class, 'show']);

app/Http/Controllers/PostController.php

<?php
  
namespace App\Http\Controllers;
  
class PostController extends Controller
{
  
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function show($id)
    {
        return 'Post ID:'. $id;
    }
}

Access URL:

http://localhost:8000/post/15



Create Route Methods:


We can create get, post, delete, put, patch methods route as shown below:

use App\Http\Controllers\UserController;
// UserController
Route::get('users', '[UserController::class, 'index']');
Route::post('users', '[UserController::class, 'post']');
Route::put('users/{id}', '[UserController::class, 'update']');
Route::delete('users/{id}', '[UserController::class, 'delete']');


We can also check how many routes are created using following command:

php artisan route:list

I hope this example helps you.