0% found this document useful (0 votes)
66 views14 pages

Basic Routing 1

The document discusses basic routing in Laravel including defining routes with Route::get(), using route parameters, optional parameters, view routes, route parameter constraints, and global constraints. The default route files are located in routes/web.php and define routes accessible by URL in the browser. Route parameters capture URI segments and are defined within {} braces. Optional parameters use a ? mark. View routes provide a shortcut to return a view without a full route. Global constraints apply a regular expression to all routes with a given parameter.

Uploaded by

Ujjwal Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views14 pages

Basic Routing 1

The document discusses basic routing in Laravel including defining routes with Route::get(), using route parameters, optional parameters, view routes, route parameter constraints, and global constraints. The default route files are located in routes/web.php and define routes accessible by URL in the browser. Route parameters capture URI segments and are defined within {} braces. Optional parameters use a ? mark. View routes provide a shortcut to return a view without a full route. Global constraints apply a regular expression to all routes with a given parameter.

Uploaded by

Ujjwal Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

BASIC ROUTING

Introduction
• The most basic Laravel routes accept a URI and a
closure, providing a very simple and expressive method of
defining routes and behavior without complicated routing
configuration files:

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
return 'Hello World';
});
The Default Route Files
• All Laravel routes are defined in your route files, which are
located in the routes directory.

• These files are automatically loaded by your application's


App\Providers\RouteServiceProvider.

• The routes/web.php file defines routes that are for your


web interface. These routes are assigned the web
middleware group.
The Default Route Files(contd.)
• For most applications, you will begin by defining routes in
your routes/web.php file.

• The routes defined in routes/web.php may be accessed


by entering the defined route's URL in your browser.

• For example, you may access the following route by


navigating to http://localhost:8000/greetings in your
browser
Route Parameters
Required Parameters - Sometimes you will need to
capture segments of the URI within your route. For
example, you may need to capture a user's ID from the
URL. You may do so by defining route parameters:

Route::get('/user/{id}', function ($id) {


return 'User '.$id;
});
Route Parameters(contd.)
• You may define as many route parameters as required by
your route.

• Route parameters are always encased within {} braces


and should consist of alphabetic characters.

• Underscores (_) are also acceptable within route


parameter names.
Route Parameters(contd.)
Route::get('/posts/{post}/comments/{comment}', function
($postId, $commentId) {
//
});
Route Parameters(contd.)
Optional Parameters - Occasionally you may need to specify
a route parameter that may not always be present in the URI.
You may do so by placing a ? mark after the parameter name.
Make sure to give the route's corresponding variable a default
value:

Route::get('/user/{name?}', function ($name = null) {


return $name;
});

Route::get('/user/{name?}', function ($name = 'John') {


return $name;
});
View Routes
• If your route only needs to return a view, you may use the
Route::view method.

• This method provides a simple shortcut so that you do not


have to define a full route or controller.

• The view method accepts a URI as its first argument and


a view name as its second argument.

• In addition, you may provide an array of data to pass to


the view as an optional third argument:
View Routes(contd.)
Route::view('/welcome', 'welcome');

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);


Using controller
use App\Http\Controllers\UserController;

Route::get('/user', [UserController::class, 'index']);


Route Parameters Constraints
Regular Expression Constraints - You may constrain the format of your route
parameters using the where method on a route instance. The where method
accepts the name of the parameter and a regular expression defining how the
parameter should be constrained:

Route::get('/user/{name}', function ($name) {


//
})->where('name', '[A-Za-z]+');

Route::get('/user/{id}', function ($id) {


//
})->where('id', '[0-9]+');

Route::get('/user/{id}/{name}', function ($id, $name) {


//
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
Route Parameters Constraints(contd.)
• Global Constraints - If you would like a route parameter
to always be constrained by a given regular expression,
you may use the pattern method. You should define these
patterns in the boot method of your App\Providers\
RouteServiceProvider class:

public function boot()


{
Route::pattern('id', '[0-9]+');
}
Route Parameters Constraints(contd.)
• Once the pattern has been defined, it is automatically
applied to all routes using that parameter name:

Route::get('/user/{id}', function ($id) {


// Only executed if {id} is numeric...
});

You might also like