Defining Basic Routes in Laravel

Defining Basic Routes in Laravel

What Are Routes in Laravel?

Routes are the entry points that tell Laravel what to do when a URL is visited.

For example:

Route::get('/hello', function () { return 'Hello, World!'; });

When someone visits http://your-app.test/hello, Laravel will return "Hello, World!".

Laravel Route File Location

All web routes are typically defined in:

routes/web.php

This is where you handle browser-based routes (frontend). For APIs, you use routes/api.php.

Basic Route Types

1. GET Route

Used when retrieving data or showing a page.

Route::get('/about', function () { return 'This is the About page.'; });

2. POST Route

Used to submit form data (like uploading a file or submitting a login form):

Route::post('/submit', function () { return 'Form submitted!'; });

3. PUT / PATCH Route

Used to update data.

Route::put('/user/update', function () { return 'User updated!'; });

4. DELETE Route

Used to delete something.

Route::delete('/post/delete', function () { return 'Post deleted!'; });

Route with Controller

Instead of putting logic directly inside the route, Laravel allows mapping routes to a controller method.

use App\Http\Controllers\PageController; Route::get('/contact', [PageController::class, 'contact']);

In PageController.php:

public function contact() { return view('contact'); }

Naming Routes

You can name a route using ->name() so that you can reference it elsewhere (like in a form).

Route::post('/form/save', [FormController::class, 'saveRecord'])->name('form/save');

Usage in Blade:

<form action="{{ route('form/save') }}" method="POST">

Route Parameters

1. Required Parameter:

Route::get('/user/{id}', function ($id) { return "User ID is: $id"; });

Visiting /user/10 returns User ID is: 10.

2. Optional Parameter:

Route::get('/post/{slug?}', function ($slug = null) { return $slug ? "Post: $slug" : "No post selected."; });

Route List

You can see all registered routes using:

php artisan route:list

It will show you:

  • Method (GET, POST, etc.)

  • URI

  • Controller

  • Name

  • Middleware

Example: Simple Laravel Form Routes Setup

// routes/web.php use App\Http\Controllers\FormController; Route::get('/', function () { return redirect('form/new'); }); Route::get('form/new', [FormController::class, 'index'])->name('form/new'); Route::post('form/save', [FormController::class, 'saveRecord'])->name('form/save');

 Summary Table

MethodUsageExample
GETRead data / show pageRoute::get('/home', ...)
POSTSubmit dataRoute::post('/form', ...)
PUTUpdate dataRoute::put('/profile', ...)
DELETEDelete dataRoute::delete('/post', ...)
ANYAccept any methodRoute::any('/anything', ...)
MATCHAccept specific methodsRoute::match(['get', 'post'], ...)

Want to explore route groups, middleware, or resource routes next?

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close