Skip to content

Routing

Here is an example of routing documentation that covers the topics you mentioned:

Basic Routing

Laravel allows you to define routes for your application using a simple and expressive syntax:

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

Route::post('/', function () {     
    // ...
});

Route::put('/', function () {     
    // ...
});

Route::delete('/', function () {     
    // ...
});

This route responds to GET requests to the root URL of your application and returns a view called welcome.blade.php.

Redirect Routes

You can easily redirect one route to another using the Route::redirect method:

Route::redirect('/there');

This will redirect to /there.

View Routes

You can return views directly from a route using the Route::view method:

Route::view('/hello', 'hello', ['name' => 'John']);

This will return the hello.blade.php view with a variable $name set to 'John'.

Route Parameters

You can define route parameters by enclosing them in {} braces in the route URI:

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

This will respond to GET requests to URLs like /users/1, /users/2, etc.

Middleware

You can apply middleware to routes using the middleware method:

Route::get('/users/{id}', function ($id) {   
    return "User {$id}"; 
}, AuthenticationMiddleware::class);

This will apply the AuthenticationMiddleware middleware to the /users/{id} route.

Controllers

You can define routes that are handled by a controller method using the Route::get method:

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

This will call the index method of the UserController class when a GET request is made to /users.