Laravel - Views

Within MVC , the "V" stands for Views . It separates application logic and presentation logic. Views are stored in the resources/views directory . Typically, the view contains the HTML code that will be served by the application.
example
Take a look at the following example to understand more about views −
Step 1: Copy the following code and save it to resources/views/test.php
<html> <body> <h1> Hello, World </h1> </body> </html>
Step 2 − Add the following line to the app/Http/rout.php file to set the route for the above view.
app/http/routes.php
Route::get('/test', function() { returnview('test'); });
Step 3 − Visit the following URL to see the view output.
http://localhost:8000/test
Step 4 − The output will look as shown in the following image.
Passing Data to Views
When you create an application, you may need to pass data to views. Pass an array to view the helper function. After passing the array, we can use the key to get the value of that key in the HTML file.
example
Take a look at the following example to learn more about passing data to views.
Step 1: Copy the following code and save it to resources/views/test.php
<html> <body> <h1><?php echo $name; ?></h1> </body> </html>
Step 2 − Add the following line to the app/Http/rout.php file to set the route for the above view.
app/http/routes.php
Route::get('/test', function() { return view('test',['name'=>'Virat Gandhi']); });
Step 3 − Key name value will be passed to test.php file and $name will be replaced with this value.
Step 4 − Visit the following URL to see the view output.
http://localhost:8000/test
Step 5 − The output will look as shown in the following image.
Sharing data with all views
We have seen how we can pass data to views, but sometimes it is necessary to pass data to all views. Laravel makes this easier. There is a method named share() that can be used for this purpose. The share() method takes two arguments, a key and a value. Typically, the share() method is called from within the service provider's boot method. We can use any service provider, AppServiceProvider or our own service provider.
example
Take a look at the following example to learn more about sharing data with all views.
Step 1 − Add the following line to app/Http/rout.php file .
app/http/routes.php
Route::get('/test', function() { returnview('test'); }); Route::get('/test2', function() { returnview('test2'); });
Step 2 - Create two view files - test.php and test2.php with the same code. These are the two files that will exchange data. Copy the following code to both files. resources/views/test.php & resources/views/test2.php
<html> <body> <h1><?php echo $name; ?></h1> </body> </html>
Step 3 − Modify the load method code in app/Providers/AppServiceProvider.php file as shown below. (We have used the share method here and the data we have passed will be available to all views.) App/Providers/AppServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services.
* * @return void */ public function boot() { view()->share('name', 'Virat Gandhi'); } /** * Register any application services. * * @return void */ public function register() { // } }
Step 4 - Visit the following URLs.
http://localhost:8000/test http://localhost:8000/test2
Step 5 − The output will look as shown in the following image.