Routes in Laravel




What is Route?

Routes are used to map URLs to controller actions or closures. They define the entry points of your application, determining how HTTP requests should be handled. A route specifies a URL pattern and a corresponding action to be taken when that URL is accessed via an HTTP request (such as GET, POST, PUT, DELETE, etc.).

Laravel

Create New project

Once Composer is installed, open your terminal or command prompt and navigate to the directory where you want to create your Laravel project.

Source Code

Sample-app\routes\web.php
<?php
 
use Illuminate\Support\Facades\Route;
 
// Basic Route
Route::get('/',function() {
	return view('welcome');
});
 
Route::get('/joes',function() {
	return "<h1>Tutor Joe's</h1>";
});
 
Route::get('/about',function() {
	return "<h1>About Page</h1>";
});
 
Route::get('/contact',function() {
	return "<h1>Contact Page</h1>";
});
 
//Passing Integer Data
Route::get('/blog/{id}',function($id) {
	return "<h1>This is Blog Page {$id}</h1>";
});
 
//Passing String
Route::get('/pages/{id}',function($id) {
	if($id=="first"){
		return "<h1>First Page</h1>";
	}else if($id=="second"){
		return "<h1>Second Page</h1>";
	}else{
		return "<h1>Invalid Page</h1>";
	}
});
 
//Mutiple Parameter
Route::get('/user/{id}/{name}',function($id,$name) {
	return "<h1>User ID : {$id} & Username : {$name} </h1>";
});
 
//Regular Expression Constraints
Route::get('/users/{name}',function($name) {
	return "<h1>Welcome {$name} </h1>";
})->where("name","[A-Za-z]+");
 
Route::get('/student/{id}',function($id) {
	return "<h1>student id is {$id} </h1>";
})->where("id","[0-9]+");
 
Route::get('/userid/{id}/{name}',function($id,$name) {
	return "<h1>User ID : {$id} & Username : {$name} </h1>";
})->where('id'=>"[0-9]+","name"=>"[a-z]+");
 
//Regular Expression with Helper Methods
Route::get('/user/{id}/{name}',function($id,$name) {
	return "<h1>User ID : {$id} & Username : {$name} </h1>";
})->whereNumber('id')->whereAlpha('name');
 
Route::get("/users/{name}",function($name) {
	return "<h1>Welcome {$name} </h1>";
})->whereAlphaNumber("name");
 
Route::get("/category/{category}",function($category) {
	return "<h1>Your are in {$category} category </h1>";
})->whereIn("category",['movie', 'song', 'painting']);
 
//Global Constraints
//App\Providers\RouteServiceProvider
//Route::parttern('id', '[0-9]+')
Route::get("/user/{id}",function($id) {
	return "<h1>student id is {$id} </h1>";
});
 
//Encoded forward Slashes
Route::get("/search/{search}",function($search) {
	return $search;
})->where("search",'.*');
 
//Route groups
 
//Without groups
Route::get('gallery/photos',function() {
	return "<h3>Photo Page</h3>";
});
Route::get('gallery/videos',function() {
	return "<h3>Video Page</h3>";
});
 
//with groups Using Prefix
Route::get("gallery")->group(function(){
	Route::get('photos',function() {
		return "<h3>Photo Page</h3>";
	});
	Route::get('videos',function() {
		return "<h3>Video Page</h3>";
	});
});
 
//FallBack Route
Route::fallback(function(){
	return "<h1>404 Page Not Found...</h1>"
});
 
?>