WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Views


In MVC structure, the letter V represents Views. It isolates the application rationale and the introduction rationale. Perspectives are put away in assets/sees directory.

Example

Observe the accompanying guide to see progressively about Views −

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

<html> 

<body> 

<h1>Hello, World</h1> 

</body> 

</html>

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

app/Http/routes.php

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

return view('test'); 

}); 

Step 3 − Visit the accompanying 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 perspectives. 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 accompanying guide to see increasingly about passing information to sees −

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

<html> 

<body> 

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

</body> 

</html>

Step 2 − Add the accompanying line in app/Http/routes.php document to set the course 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 accompanying 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 perspectives. 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 accompanying guide to see progressively about offering information to all perspectives −

Step 1 − Add the accompanying 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 accompanying 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 perspectives.) 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 accompanying URLs.

http://localhost:8000/test 

http://localhost:8000/test2