Basic Routing

Basic Routing

The most basic Laravel routes accept a URI and a Closure.

Route::get('foo', function () {
    return 'Hello World';

All route files are automatically loaded by the framework. Web middleware group is assigned to the web routes. The routes in routes/api.php are stateless and are assigned the api middleware group.

Available Router Methods

The router allows us to register routes that respond to any HTTP verb:

Route::get($uri, $callback);

Route::post($uri, $callback);

Route::put($uri, $callback);

Route::patch($uri, $callback);

Route::delete($uri, $callback);

Route::options($uri, $callback);

Sometimes we may need to register a route that responds to multiple HTTP verbs.

Route::match(['get', 'post'], '/', function () {

We may even register a route that responds to all HTTP verbs using the any method:

Route::any('/', function () {

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.

<form method="POST" action="/profile">

Redirect Routes

Also used for SEO purposes.

View Routes

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

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