Creating and using a basic view in laravel

In this recipe, we’ll see some basic view functionality and how we can include views in
our app.

Getting ready

For this recipe, we need a standard Laravel installation.

How to do it…

Follow these steps to complete the recipe:
1. In the app/views directory, create a folder name myviews.
2. In the new myviews directory, create two files: home.php and second.php.
3. Open home.php and add the following code in HTML:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the Home page!</h1>
<p>
<a href=”second”>Go to Second Page</a>
</p>
</body>
</html>

4. Open the second.php file and add the following code in HTML:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Second Page</title>
</head>
<body>
<h1>Welcome to the Second Page</h1>
<p>
<a href=”home”>Go to Home Page</a>
</p>
</body>
</html>

5. In our app/routes.php file, add the routes that will return these views:
Route::get(‘home’, function()
{
return View::make(‘myviews.home’);
});
Route::get(‘second’, function()
{
return View::make(‘myviews.second’);
});
6. Test the views by going to http://{your-server}/home (where your-server is
our URL) and clicking on the link.

How it works…

All of the views in Laravel are kept in the app/views directory. We start by creating two files
that will hold our HTML. In this example, we’re creating static pages, with each view holding its
own full HTML markup.
In our routes file, we then return View::make(), with the name of the view passed in. Since
our views are in a subdirectory of the views directory, we use the dot notation.

Leave a Reply

Your email address will not be published. Required fields are marked *