In this tutorial you will learn about the Laravel Views and its application with practical example.
What is View?
View is one of the key component in any Model-View-Controller(MVC) framework.Views are meant to hold the presentation logic in any Laravel
application. Views separates presentation logic from the application logic. A view file usually contains the information being presented to a user. View can be a complete web page or parts of page like header and footer.
In Laravel, views are stored in resources/views directory. View for a basic web page can be directly called from route.
Creating a view
Step 1:- Create a new file hello.php and save it at resources/views directory.
Step 2:- Copy and paste following code in the hello.php
1 2 3 4 5 |
<html> <body> <h1>Hello, Laravel</h1> </body> </html> |
Step 3:- Copy and paste following code in the app/Http/routes.php to configure the route for the view created above.
1 2 3 |
Route::get('/hello', function(){ return view('hello'); }); |
Step 4:- Now open the following URL in the browser to see the output.
http://localhost:8000/hello
Passing Data to a view
If you want to pass some data to a view, it is possible by simply passing a data array consist in key/value pair.
Example:-
Step 1:- Open the “hello.php” file we created above, and update it with following code.
1 2 3 4 5 6 7 |
<!-- located in resources/views/hello.php --> <html> <body> <h1>Hello, <?php echo $name; ?></h1> </body> </html> |
Step 2:- Open app/Http/routes.php and re-configure the route as below –
1 2 3 4 |
Route::get('/hello', function(){ $data = array('name' => 'Laravel'); return view('hello',$data); }); |
Step 3:- Inside the view, we can retrieve each of the data value by its corresponding key. In “hello.php”, $name will be replaced by it’s value.
Step 4:- Again open the following URL in the browser to see the output.
http://localhost:8000/hello
Sharing Data with all Views
Sometimes we may want to share some common data across all the views. In Laravel, we can do it using share() method. Typically share() method is called from boot method of service provider.
Example:-
Step 1:- Create two new views as exp1.php and exp2.php, sharing the common variable $name.
Step 2:- Open the exp1.php and exp2.php, and copy the following code in corresponding files.
resources/views/exp1.php
1 2 3 4 5 6 7 8 |
<html> <head> <title>Example 1</title> </head> <body> <p>Hello, <?php echo $name ?></p> </body> </html> |
resources/views/exp2.php
1 2 3 4 5 6 7 8 |
<html> <head> <title>Example 2</title> </head> <body> <p>Hello, <?php echo $name ?></p> </body> </html> |
Step 3:- Open app/Http/routes.php and define corresponding routes exp1 and exp2.
1 2 3 4 5 6 7 |
Route::get('exp2', function(){ return view('exp1'); }); Route::get('exp2', function(){ return view('exp2'); }); |
Step 4:- Open app/Providers/AppServiceProvider.php file and call share() method inside the boot method as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function boot() { view() -> share('name', 'Laravel'); } public function register() { // } } |
Here we have defined a common variable $name, that can be shared across both the views we created.
Step 5:- Now visit the following URLs in the browser to see the output.
http://localhost:8000/exp1
http://localhost:8000/exp2