WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Views


In MVC structure, the letter V represents Views. It separates the application rationale and the presentation rationale. views are put away in assets/sees directory.

Example

Observe the following guide to see progressively about Views −

Step 1 − Copy the following code and spare it at resources/sees/test.php

<html> 

<body> 

<h1>Hello, World</h1> 

</body> 

</html>

Step 2 − Add the following line in app/Http/routes.php document to set the route for the above view.

app/Http/routes.php

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

return view('test'); 

}); 

Step 3 − Visit the following URL to see the yield of the view.

http://localhost:8000/test 

Passing Data to Views

While building application it might be required to pass information to the views. Pass a cluster to see assistant capacity. Subsequent to passing an exhibit, we can utilize the way to get the estimation of that key in the HTML file.

Example

Observe the following guide to see increasingly about passing information to sees −

Step 1 − Copy the following code and spare it at resources/sees/test.php

<html> 

<body> 

<h1><?php reverberation $name; ?></h1> 

</body> 

</html>

Step 2 − Add the following line in app/Http/routes.php document to set the route for the above view.

app/Http/routes.php

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

return view('test',['name'=>'Virat Gandhi']); 

}); 

Step 3 − The estimation of the key name will be passed to test.php document and $name will be supplanted by that value.

Step 4 − Visit the following URL to see the yield of the view.

http://localhost:8000/test 

Sharing Data with all Views

We have perceived how we can pass information to sees however now and again, there is a need to pass information to every one of the views. Laravel makes this more straightforward. There is a technique called share() which can be utilized for this reason. The share() strategy will take two contentions, key and esteem. Ordinarily share() technique can be called from boot strategy for specialist co-op. We can utilize any specialist organization, AppServiceProvider or our very own administration provider.

Example

Observe the following guide to see progressively about offering information to all views −

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

app/Http/routes.php

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

return view('test'); 

}); 

Route::get('/test2', function(){ 

return view('test2'); 

}); 

Step 2 − Create two view documents — test.php and test2.php with a similar code. These are the two documents which will share information. Duplicate the following code in both the documents. resources/sees/test.php & assets/sees/test2.php

<html> 

<body> 

<h1><?php reverberation $name; ?></h1> 

</body> 

</html>

Step 3 − Change the code of boot technique in the document app/Providers/AppServiceProvider.php as appeared as follows. (Here, we have utilized offer technique and the information that we have passed will be imparted to every one of the views.) app/Providers/AppServiceProvider.php

<?php 

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 

class AppServiceProvider extends ServiceProvider { 

/** 

* Bootstrap any application administrations. 

* 

* @return void 

*/ 

public function boot(){ 

view()- >share('name', 'Virat Gandhi'); 

} 

/** 

* Register any application administrations. 

* 

* @return void 

*/ 

public function register(){ 

// 

} 

}

Step 4Visit the following URLs.

http://localhost:8000/test 

http://localhost:8000/test2