Saturday, June 21, 2025
HomePHPLaravel Routing - PHPGurukul

Laravel Routing – PHPGurukul


Routes are your Laravel Software’s entry level, defining the way it responds to completely different URLS and HTTP requests.

A route is a technique to specify the URL patterns your software ought to reply to what motion to take when a selected URL is accessed. Routes are outlined within the routes folder, notably in internet.php (Internet routes) and api.php (API routes). Every route has a URL path and an related callback operate or controller technique that handles the request.

Primary Routing

  1. Open the routes/internet.php file.
  2. Outline a route utilizing Route::get to specify that this route ought to reply to GET Requests.

Instance: Primary route that returns textual content.

Rationalization:

Route::get: This specifies that this route solely responds to GET Requests.

'/whats up': That is the URL path. If you go to HTTP://localhost:800/whats up, this route will likely be triggered.

Callback operate: The nameless operate (operate () {……}) returns ‘Welcome, to PythonCodeVerse’ to person.

To see the output, begin your Laravel server with:

Open your browser and go to http://localhost:8000/whats up. It’s best to see “Welcome, to PythonCodeVerse” displayed on the display.

HTTP Requests: GET, POST, PUT, DELETE

HTTP requests enable purchasers (Like browsers) to speak with the server, every with its particular function:

GET: Retrieves knowledge from the server, usually used for displaying pages.

POST: Sends knowledge to the server, generally used for submitting varieties

PUT: Replace current knowledge on the server.

DELETE: Removes knowledge from the server.

Defining GET and POST Routes

In Laravel, you possibly can outline routes for various HTTP request varieties by changing Route::get with Route:put up, Route:put, or Route::delete.

Instance: POST Route

On this instance, the /submit route will reply to POST requests. As proven beneath, you’d usually use a type to make this request.

Right here, we set technique=”POST” to make a POST request to the /submit route.

Discover the @csrf directive, which provides a CSRF (Cross-Web site Request Forgery) token for safety. Laravel requires this token for all POST, PUT, and DELETE requests to stop unauthorised entry.

PUT and DELETE Routes

To deal with updates and deletions, we use PUT and DELETE requests, which are sometimes utilized in RESTful purposes.

Identical to put up, you’d usually make PUT and DELETE requests by way of type or JavaScript, setting the tactic accordingly. Laravel makes it simple to specify inside a type by utilizing hidden fields.

Instance: PUT request with a type

Within the above instance, we use @technique('PUT') to point that this manner needs to be handled as a PUT request. This tells Laravel to match the shape submission to the /replace route outlined with Route::put

Dynamic Routes with Route Parameters

Laravel permits you to create dynamic routes by utilizing parameters within the URL. That is notably helpful for conditions the place you need to go particular data, like person id or product id, product identify, by way of the URL.

Instance: Defining a route with a parameter

On this instance:

  • {id} is a route parameter. Laravel will seize this worth from the URL and go it to the callback operate because the $id variable.
  • If you entry http://localhost:8000/person/1, Laravel will show “Consumer id:1” as a result of the $id parameter is ready to 1.

A number of Parameters

Instance: A route with two parameters

This route will reply to URLS like http://loclahost:8000/person/1/put up/5, displaying “Consumer id:1 Publish id:5”.

Non-compulsory Parameters

Generally, you might need to make a route parameter elective. To do that, add a ? after the parameter identify and supply a default worth within the callback operate.

Instance: Defining a route with an elective parameter

On this instance, when you go to http://localhost:8000/person/john, it shows “Hiya, john!” when you omit the identify

Ex: http://localhost:8000/person , It shows “Hiya, Visitor!”

Named Routes

Named routes will let you assign a reputation to a route, making it simple to reference in your software. That is notably helpful when producing URLS or redirecting to particular routes.

Instance: Defining a named route

You’ll be able to then reference this route by its identify elsewhere in your software, like in hyperlinks or redirects.

Instance: Producing a URL to a named route.

Grouping Routes

Laravel helps you to group routes, which is beneficial for organising routes that share a typical function, like middleware or prefixes.

Instance: Grouping routes with a prefix.

With this setup, you possibly can entry the routes at http://localhost:8000/admin/dashboard and http://localhost:8000/admin/settings. The prefix technique mechanically prepends “admin” to every route within the group.

Route Middleware

Middleware permits you to filter requests by making use of a login earlier than the requests attain their supposed vacation spot route. Widespread use instances embrace authentication and entry management.

Instance: Defending a route with authentication middleware.

On this instance, the auth middleware ensures that solely authenticated customers can entry the /profile route.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments