10-Apr-2023
.
Admin
Hi Guys,
In this blog , I will explain you step by step laravel 7/6 routing tutorial.we will show how to create new route in laravel 7/6. we will also create how to create route in laravel controller.
we will create step by step process of how to create route in laravel 7/6. Routing is one of the essential concepts in Laravel. Routing in Laravel allows you to route all your application requests to its appropriate controller.
The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.
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.
I will describe 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 ,i will create very simple route and will let you know how you can access iso let's create simple route using following line adding in route file:
following path:/routes/web.php
Route::get('simple-route-example', function () {
return 'This is Simple Route Example of Nicesnippets.com';
});
Access Url
http://localhost:8000/simple-route-example
Route with Call View File
Now,we can create route with directly call view blade file from route directly.
following path:/routes/web.php
Route::view('call-view-route', 'index');
following path:/resources/views/index.php
<h1>His Is Call View Route Example of Nicesnippets.com</h1>
Access Url
http://localhost:8000/call-view-route
Route with Controller Method
Here,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
following path:/routes/web.php
Route::get('route-with-controller', 'MainController@index')
following path:/app/Http/Controllers/MainController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MainController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('index');
}
}
following path:/resources/views/index.php
<h1>His Is Route with Controller Method Example of Nicesnippets.com</h1>
Access Url
http://localhost:8000/route-with-controller
Create Route with Parameter
Now, I will create simple route with passing parameters. you can create dynamic route with your controller.
following path:/routes/web.php
Route::get('route-with-parameter/{$name}', 'ClientController@show')
following path:/app/Http/Controllers/MainController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ClientController extends Controller
{ /**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function show($name)
{
return 'Client Name:'. $name;
}
}
Access Url
http://localhost:8000/route-with-parameter/nicesnippets
Create Route Methods
Here,You can create get, post, delete, put, patch methods route as bellow i created. you can understand how it works.
so, let's see here route examples:
Route::get('players', 'PlayerController@index');
Route::post('players', 'PlayerController@post');
Route::put('players/{id}', 'PlayerController@update');
Route::delete('players/{id}', 'PlayerController@delete');
Get Route List in Command Prompt
We will show all route list in command prompt.You can also check how many routes i created using following command
php artisan route:list
Output like as Here:
It will help you...
#Laravel 7
#Laravel
#Laravel 6