Monday, February 17, 2020

How to Create Routes in Laravel 6-Easy Tutorial

How to Create Routes in Laravel 6-Easy Tutorial

Today in this tutorial I will explain the Laravel routing tutorial step by step. We will learn how to create a new route in Laravel. You can easily create a later path in the Laravel application. I will also show how to create a route in the Laravel controller.

Let us see step by step how to create the first route in Laravel and understand the Laravel routing shortly.

What is Laravel Routing?


With routing, you can create a request URL for your application. You can design an HTTP request set as a POST request, GET request, PUT request, and DELETE request using Laravel routing.

You can simply create a route in the web.php file in a route folder. You can create your route list in various ways in the web.php file. I will show you step by step how to create it and how it works. So let's see a step-by-step explanation.

Create Simple Route:


Here we create a very simple route and tell you how to access it. So let's create a simple route with the following line by adding the path file:

routes/web.php

Route::get('simple-route', function () {

    return 'This is Simple Route Example of ItSolutionStuff.com';

});

Access URL:

http://localhost:8000/simple-route


Route with Call View File:


You can create a route directly from the route using the Call View Blade file. You can see the following route example:

routes/web.php

Route::view('my-route', 'index');

resources/views/index.php

Route::view('my-route', 'index');

Access URL:

http://localhost:8000/my-route

Route with Controller Method:


Now you can create a route using the call controller method. So you can simply create the controller method and call this method with your route as follows:

routes/web.php

Route::get('my-route', 'TestController@index');

app/Http/Controllers/TestController.php

<?php

  

namespace App\Http\Controllers;

  

class TestController 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 Example of ItSolutionStuff.com</h1>

Access URL:

http://localhost:8000/my-route


Create Route with Parameter:


Here we create a simple route with step parameters. You can create a dynamic route with your controller. So let's create as follows.

routes/web.php

Route::get('users/{id}', 'UserController@show');

app/Http/Controllers/UserController.php

<?php

  

namespace App\Http\Controllers;

  

class UserController extends Controller

{

  

    /**

     * Show the application dashboard.

     *

     * @return \Illuminate\Contracts\Support\Renderable

     */

    public function show($id)

    {

        return 'User ID:'. $id;

    }

}

Access URL:

http://localhost:8000/users/21

Create Route Methods:


You can use the methods below to get, publish, delete, set, and patch paths. You can understand how it works.

So let's look at the following examples of routes:

Route::get('users', 'UserController@index');

Route::post('users', 'UserController@post');

Route::put('users/{id}', 'UserController@update');

Route::delete('users/{id}', 'UserController@delete');

You can also check how many routes I created using the following command:

php artisan route:list


You can see the list of all routes.

I hope This Could help you...

0 comments:

Post a Comment

Please don't enter any spam link in the comment box.