Routing in Laravel
The following shows how HTTP routes are defined, typically in routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
$routeList = 'To list all routes defined here ' .
'(and a few others defined by Laravel), ' .
'enter: php artisan route:list' . PHP_EOL;
// path parameters (note we are passing an anonymous
// function so need to give it access to $routeList)
Route::get('/greet/{name}', function ($name) use ($routeList) {
return 'Greeting ' . $name . "<br/><br/>" . $routeList;
});
// handling redirects
Route::get('/goto/{name?}', function ($page) use ($routeList) {
if ($page == 'welcome') {
return redirect('/');
}
if ($page == 'named') {
// this redirect ignores the URL path (defined by
// namedRoute) and instead refers to the name of the Route instance
return redirect()->route('namedRoute');
}
return 'Go to page ' . $page . "<br/><br/>" . $routeList;
});
$namedRoutes = "Note that Route::get returns an instance of " .
"Route, so one can set the name of the route and have it " .
"listed on entering php artisan route:list<br/><br/>";
Route::get('/named', function () use ($namedRoutes) {
return $namedRoutes . "<br/><br/>";
})->name('namedRoute');
// a fallback route
Route::fallback(function () use ($routeList) {
return "This is the fallback route<br/><br/>" . $routeList;
});