Basic Routing in Laravel 9!

Basic Routing in Laravel 9!

Introduction

The Laravel framework is a PHP-based framework for developing web applications that include capabilities such as routing. I'm sure you're probably wondering, "What is Routing?", What does it have to do with anything? What is the mechanism behind it? Hold on, I've got you.

Prerequisites

  • Knowledge of OOP PHP

  • Basic understanding of the Laravel framework

  • Basic understanding of controllers, their purpose, and creating them in Laravel

  • Familiarity with HTTP methods

If you match these criteria, it's time to fine-tune your routing skills!

What is Routing?

Routing is a mechanism for creating URL requests. Simple! Laravel routes allow you to bind your controllers to specific requests. A basic Laravel route receives a URL and a closure, making defining routes in methods a breeze.

use Illuminate\Support\Facades\Route;

Route::get('/greeting',function(){
return 'Hello world';
});

Laravel creates four main files by default, each of which defines a route. api.php, web.php, channels.php, and console.php are the files in question. These files contain routes for the application's various tiers. For example, api.php has API routes (for more information on APIs, see my earlier blog article), web.php contains web routes, and so on.

Creating routes in Laravel using HTTP methods.

These HTTP methods are supported by Laravel for creating routes.

  • GET, POST, PUT, PATCH, DELETE and OPTIONS
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

It also supports the MATCH method which allows your route responds to multiple HTTP methods and also the ANY method which allows your route to respond to any HTTP method.

Route::match(['get', 'post'], '/', function() {
           //
});
Route::any( '/', function() {
           //
});

However, if you're defining routes using the any or match method, ensure to define your GET, POST, PUT, PATCH, DELETE, and OPTIONS first to guarantee that the incoming request is routed correctly

If you need to make a new file, go to app/Providers/RouteServiceProvider.php and register it. The RouteServiceProvider is in charge of loading the application's routes. The URI prefix is automatically added, so you don't have to include it in every file, but you can change it if you want.

Route::resource

The Route::resource function is a RESTful Controller that generates all of an application's fundamental routes and can be managed easily using the controller class. It accepts two arguments: the basic incoming request URI (Uniform Resource Identifier) and the controller class name used to handle the request.

use App\Http\Controllers\StudentController;
Route::resource('student', StudentController::class);

You may also pass an array to the resources method to register many resource controllers at once.

Route::resources([
      'student'=> StudentController::class,
      'posts' => PostController::class,
]);

Conclusion

This is just a simple introduction to routing in Laravel. Other efficient ways to route requests exist in Laravel that were not covered in this article. Visit the official Laravel documentation for additional information. I hope you enjoyed this article like I did, bye for now!