Basic Routing

The routes/web.php file defines routes that are for web interface. The routes defined in web.php, CSRF(Cross-site request forgery) token required for Post,Put and Delete method.

The most basic Laravel routes accept a URI and a Closure, providing a very simple and expressive method of defining routes:

Route::get('/', function () {
    return view('welcome');
});

CSRF Protection : Any HTML forms pointing to POST, PUT, PATCH, or DELETE routes that are defined in the web routes file should include a CSRF token field. Otherwise, the request will be rejected.

<form method="POST" action="/profile">
    @csrf
    ...
</form>

Redirect Routes

Redirect Routes method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect.

Route::redirect('/here', '/redirect');

View Routes

If your route only needs to return a view, you may use the Route::view method.

Route::view('/welcome', 'welcome');

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.

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

Optional Parameters

Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional.

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

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]+']);