HDTuto.com

Laravel 7.x and 6.x Routing Tutorial



This article will provide example of laravel 6 routing tutorial. Here you will learn routing in laravel 6 example. you can understand a concept of route in laravel 6 controller. if you have question about laravel 6 route tutorial then i will give simple example with solution. Let's see bellow example how to create route in laravel 6.

Let's see step by step process of how to create first route in laravel and understanding of laravel routing with brief.

What is Laravel Routing?

Using Routing you can create a request URL for your application. you can design set of HTTP request like POST Request, GET Request, PUT Request and DELETE Request using routing in laravel.

You can easily create route in web.php file inside a routes folder. in web.php file you can create your route list there are several ways. i will show you bellow step by step how you can create it and how it works, so let's see step by step explanation.

Create Simple Route:

Here, we will create very simple route and will let you know how you can access it. so let's create simple route using following line adding in route 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 route with directly call view blade file from route directly. You can see bellow created route example:

routes/web.php

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

resources/views/index.php

<h1>his is Simple Route Example of ItSolutionStuff.com</h1>

Access URL:

http://localhost:8000/my-route

Route with Controller Method:

Now, you can create route with call controller method so, you can simply create controller method and call that method with your route as like bellow:

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 will create simple route with passing parameters. you can can create dynamic route with your controller. so let's create as bellow

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 create get, post, delete, put, patch methods route as bellow i created. you can understand how it works.

So, let's see bellow route examples:

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 following command:

php artisan route:list

Output like as bellow:

You can see above created routes.

You can also create resource routes as i written tutorial on it. You can see here: Create Resource Routes in Laravel.

I hope it can help you...




February 23, 2020 | Category : LaravelLaravel 6
Related Posts



Blog Search


Copyright © 2020 HDTuto.com. All rights reserved.