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.
While building your application, you may need to pass data to views.
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
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
.
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:
Laravel, famous for its elegant syntax and powerful features, also provides a solid foundation for modular architecture. There…
Cisco and Splunk are helping customers accelerate their journey to the Security Operations Center (SOC) of the future with…
Ransomware has dominated the news for the last two years. Most people are well aware that attacks…
An ophthalmoplasty operation using the Apple Vision Pro commercial viewer was performed at the Catania Polyclinic…
Developing fine motor skills through coloring prepares children for more complex skills like writing. To color…
The naval sector is a true global economic power, which has navigated towards a 150 billion market...
Last Monday, the Financial Times announced a deal with OpenAI. FT licenses its world-class journalism…
Millions of people pay for streaming services, paying monthly subscription fees. It is common opinion that you…