Laravel Course

- Passing Data to Views
- Sharing Data with all Views

Understanding Views

Views (or Templates) in Laravel are stored in the "resources/views" directory. They are '.php' files that describe how some output should look, and can contain HTML, CSS, JavaScript, and PHP content.
In Laravel, there are two formats of view you can use: Blade (with ".blade.php" extension in file name) rendered with the Blade engine; or simple ".php" (which will not parse the Blade Template statements).

- Example of a simple Blade view:
1. Copy the following code and save it in a resources/views/light.blade.php file.
<html>
<body>
<h1>View is an effect, Vision is the source</h1>
</body>
</html>
2. Add the following line in routes/web.php file to set the route for the above view.
Route::get('/light', function(){
 return view('light');
});
3. Visit the following URL to see the output of the view.
//localhost:8000/light
- The output will appear as shown in the following image.
laravel Simple View
If the view file is into a sub-folder in "resources/views" directory, for example "resources/views/pages/name.blade.php", add the sub-folder name before file name:
return view('pages.name');
If you need to determine if a view exists, you may use the exists() method of the View facade. The method return true if the view exists:
use Illuminate\Support\Facades\View;

if(View::exists('pages.test')){
 //
}

Passing Data to Views

You can use variables in the view template file, with this syntax: {{$variable_name}} , then pass an array with key/value pairs as a second argument in the view() method.
The "key" in array must be the same as the name of the variable in template.

- Example of a simple Blade view:
1. Copy the following code and save it in a resources/views/greeting.blade.php file.
<html>
<head>
<title>{{$title}}</title>
</head>
<body>
<h1>Hello {{$name}}</h1>
</body>
</html>
2. Add the following line in routes/web.php file to set the route for the above view.
Route::get('/greeting', function(){
 $vars =['title'=>'Greetings', 'name'=>'Me'];
 return view('greeting', $vars);
});
3. Visit the following URL to see the output of the view.
//localhost:8000/greeting
- The output will appear as shown in the following image.
laravel Greetings View

Sharing Data with all Views

If you want to pass same data to all the views, use the view()->share() method.
The share() method will take two arguments: key and value.
Typically share() method can be called from boot() method of service provider.
We can use any service provider, like: AppServiceProvider or our own service provider.

- Example:
1. Create two view files: test.blade.php and test2.blade.php with the same code, in the "resources/views/" directory.
- Copy the following code in both the files.
<html>
<body>
<h1>Share {{$state}}</h1>
<h2>Receive {{$gift}}</h2>
</body>
</html>
2. Change the code of boot() method in the file app/Providers/AppServiceProvider.php as shown below. (Here, we have used the share() method, and the data that we have added will be shared with all the views).
<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {
 //Bootstrap any application services.
 //@return void
 public function boot(){
 view()->share('state', 'Peace');
 view()->share('gift', 'Love');
 }

 //Register any application services.
 //@return void
 public function register(){
 //
 }
}
3. Add the following lines in routes/web.php file.
Route::get('/test', function(){
 return view('test');
});
Route::get('/test2', function(){
 return view('test2');
});
4. Visit the following URLs:
//localhost:8000/test

//localhost:8000/test2
- The output for both URLs will appear as shown in the following image.
laravel Greetings View

- Documentation: Laravel - Views

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Laravel Views

Last accessed pages

  1. Add, Change, and Remove Attributes with jQuery (46356)
  2. Node.js Move and Copy file (28419)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141747)
  4. Rectangle, Oval, Polygon - Star (3322)
  5. PHP PDO - prepare and execute (9187)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (471)
  2. CSS cursor property - Custom Cursors (78)
  3. The Mastery of Love (69)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)