In Laravel, you can use the
last
method of theRequest
object to retrieve the last segment of the URL:
use Illuminate\Http\Request;
Route::get('/{any}', function (Request $request) {
$lastSegment = $request->segment(count($request->segments()));
return $lastSegment;
});
Alternatively, you can use the basename
function to retrieve the last segment of the URL:
use Illuminate\Http\Request;
Route::get('/{any}', function (Request $request) {
$path = $request->path();
$lastSegment = basename($path);
return $lastSegment;
});