Concept of Laravel Views Tutorial

In MVC framework, the letter “V” stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.

Example

Step 1 − Add the following within the resources/views/test.php

<html>
   <body>
      <h1>Hello, World</h1>
   </body>
</html>

Step 2 – Add the next line in routes/web.php file to set the route for the above view.

Route::get('/test', function() );

Step 3 – Visit the next URL to see the output of the view.

http://YOUR_DOMAIN/test

Passing Data to Views

While constructing application it could be required to pass data to the views. Pass an array to view helper function. After passing an array, we are able to use the key to get the value of that key within the HTML file.

Example

Observe the next example to understand extra about passing data to views –

Step 1 − Copy the following code and put it aside at

resources/views/test.php

<html>
   <body>
      <h1><?php echo $name; ?></h1>
   </body>
</html>

Step 2 – Add the next line in app/Http/routes.php file to set the route for the above view.

routes/web.php

Route::get('/test', function() 
   return view('test',['name'=>'Abhay Anand']);
);

Step 3 – The value of the key name might be passed to test.php file and $name might be replaced by that value.

Step 4 − Visit the next URL to see the output of the view.

https://YOUR_DOMAIN/test

Sharing Data with all Views

We have seen how we are able to pass data to views however at times, there’s a must pass data to all of the views. Laravel makes this less complicated. There is a method known as share() which can be used for this purpose. The share() method will take two arguments, key and value. Typically share() method might be known as from the boot method of the service provider. We can use any service provider, AppServiceProvider or our own service provider.

Example

Observe the next example to understand extra about sharing data with all views –

Step 1 − Add the next line in routes/web.php file.

routes/internet.php

Route::get('/test', function() );

Route::get('/test2', function() {
   return view('test2');
});

Step 2 – Create two view files – check.php and test2.php with the same code. These are the 2 files which is able to share data. Copy the next code in each the files. resources/views/test.php & resources/views/test2.php

<html>
   <body>
      <h1><?php echo $name; ?></h1>
   </body>
</html>

Step 3 – Change the code of boot method within the file

app/Providers/AppServiceProvider.php as shown below. (Here, now we have used share method and the information that now we have handed might be shared with all of the 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 next URLs.

http://localhost:8000/test

http://localhost:8000/test2

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com