Routing in Laravel(Laravel Routes)

Routing in Laravel serves as a fundamental aspect of directing HTTP requests to the appropriate application logic. It facilitates the mapping of URIs to controller actions, providing a clear structure for handling incoming requests.

Basic Routing

In Laravel, routing is defined in the routes/web.php file for web routes and routes/api.php for API routes. Here’s how basic routes are defined:

<?php

Route::get(‘/’, function () {

return view(‘welcome’);

});

Route::get(‘/about’, ‘AboutController@index’);

 

In this example:

  • The / route renders a view named welcome.
  • The /about route directs to the index method of AboutController.

 

Route Parameters

Route parameters allow dynamic URIs, capturing values that are passed to controller actions:

<?php

Route::get(‘/user/{id}’, ‘UserController@show’);

// Accessing the parameter in the controller

public function show($id) {

$user = User::find($id);

return view(‘user.profile’, [‘user’ => $user]);

}

 

Named Routes

Named routes provide a convenient way to generate URLs or redirects:

<?php

Route::get(‘/dashboard’, ‘DashboardController@index’)->name(‘dashboard’);

// Generating URL

$url = route(‘dashboard’);

 

API Routes

API routes, defined in routes/api.php, are typically stateless and used for handling API requests:

<?php

Route::middleware(‘auth:api’)->group(function () {

Route::get(‘/user’, ‘UserController@profile’);

Route::post(‘/post’, ‘PostController@store’);

});

Also Read:-

Basic Shape Tools in Photoshop

Understanding Array Key Resetting in PHP

Also Visit:-

https://inimisttech.com/

 

Leave a Reply