Laravel: How Create route with view?

In Laravel, you can create a route that returns a view by using the Route facade and the view method. Here are the steps to create a route with a view in Laravel:

  1. Create a Blade View: First, you need to create a Blade view file in the resources/views directory or any subdirectory within it. For this example, let's create a view called welcome.blade.php:

    html
  • <!-- resources/views/welcome.blade.php --> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome to Laravel</h1> </body> </html>
  • Define a Route: Next, you'll define a route in the routes/web.php file. You can use the Route::view method to create a route that directly returns the view:

    php
    1. // routes/web.php Route::get('/welcome', function () { return view('welcome'); });

      In this example, when someone visits the /welcome URL in their browser, the welcome.blade.php view will be displayed.

    2. Access the View: You can now access the view by navigating to http://yourdomain.com/welcome in your web browser.

    That's it! You've created a route in Laravel that returns a view. You can customize the view and route as needed for your application.

    Comments