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:
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 calledwelcome.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
// routes/web.php Route::get('/welcome', function () { return view('welcome'); });
In this example, when someone visits the
/welcome
URL in their browser, thewelcome.blade.php
view will be displayed.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
Post a Comment