Articles

Laravel: What are laravel Views

In the MVC framework, the letter "V" stands for Views, and in this article we will see how to use views in Laravel. Separate application logic and presentation logic. Views are stored in the resources/views directory. Typically, the view contains the HTML that will be rendered in the browser.

Example

Let's see the following example to understand more about Views

1 – Copy the following code and save it in resources/views/test.blade.php

<html>
   <body>
      <h1>Laravel Blog Innovazione</h1>
   </body>
</html>

2 – Add the following line in the file routes / web.php to set the path for the view above.

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

3 – In the browser we open the page at the URL to see the output of the view.

http://localhost:8000/test

As a result we will see the writing “Laravel Blog Innovazione” in title h1

The address http://localhost:8000/test set in the browser will lead to the route test specified in the second point, calling up the view test.blade.php specified in point 1.

Passing data to views

While building your application, you may need to pass data to views. 

Example

To see how data is passed to views, let's proceed with an example:

1 – Copy the following code and save it in resources/views/test.blade.php

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

2 – We add the following line in the file routes / web.php to set the path for the view above.

Route::get('/test', function() {
   return view('test',[‘name’=>’Laravel Blog Innovazione’]);
});

3 – The value corresponding to the key 'name' will be passed to the file test.blade.php and $name will be replaced by that value.

4 – Let's visit the following URL to see the output of the view.

http://localhost:8000/test

5 – The output will appear in the browser with the same writing as in the first example, i.e. the writing “Laravel Blog Innovazione” in title h1

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.

Sharing data with all views

We have seen how we can pass data to views, but sometimes we need to pass data to all views. Laravel makes it easier. There is a method called share() which can be used for this purpose. The method share() will take two arguments, key and value. Generally the method share() can be called from the service provider's startup method. We can use any service provider, AppServiceProvider or ours service provider.

Example

See the following example to understand more about sharing data with all views –

1 – Add the following line in the file app/Http/routes.php .

app/Http/paths.php

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

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

2 – We create two view files: test.blade.php e test2.blade.php with the same code. These are the two files that will share the data. Copy the following code into both files. resources/views/test.blade.php e resources/views/test2.blade.php

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

3 – Change the boot method code in the file app/Providers/AppServiceProvider.php as shown below. (Here, we have used the sharing method and the data we passed will be shared with 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', 'Laravel Blog Innovazione');
   }

   /**
      * Register any application services.
      *
      * @return void
   */

   public function register() {
      //
   }
}

4 - Visit the following URLs.

http://localhost:8000/test
http://localhost:8000/test2

5 – The output will appear in the browser with the same writing as in the first and second examples, i.e. the writing “Laravel Blog Innovazione” in title h1

Ercole Palmeri

They may also be interested in these items:

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.

Latest Articles

Exploring Laravel's Modular Architecture

Laravel, famous for its elegant syntax and powerful features, also provides a solid foundation for modular architecture. There…

May 9, 2024

Cisco Hypershield and acquisition of Splunk The new era of security begins

Cisco and Splunk are helping customers accelerate their journey to the Security Operations Center (SOC) of the future with…

May 8, 2024

Beyond the economic side: the unobvious cost of ransomware

Ransomware has dominated the news for the last two years. Most people are well aware that attacks…

May 6, 2024

Innovative intervention in Augmented Reality, with an Apple viewer at the Catania Polyclinic

An ophthalmoplasty operation using the Apple Vision Pro commercial viewer was performed at the Catania Polyclinic…

May 3, 2024

The Benefits of Coloring Pages for Children - a world of magic for all ages

Developing fine motor skills through coloring prepares children for more complex skills like writing. To color…

May 2, 2024

The Future is Here: How the Shipping Industry is Revolutionizing the Global Economy

The naval sector is a true global economic power, which has navigated towards a 150 billion market...

May 1, 2024

Publishers and OpenAI sign agreements to regulate the flow of information processed by Artificial Intelligence

Last Monday, the Financial Times announced a deal with OpenAI. FT licenses its world-class journalism…

April 30 2024

Online Payments: Here's How Streaming Services Make You Pay Forever

Millions of people pay for streaming services, paying monthly subscription fees. It is common opinion that you…

April 29 2024