Laravel Views


Laravel Views – The Laravel Views is used to seperated the application logic and the presentation logic. In MVC farmework, V stand for view. Views file stored in resource/views directory.


Laravel Views.

Let us understand how to use laravel views.

Function:-

There are some followings function available in laravel views.

  • 1. Creating Views.
  • 2. Passing Data To Views.
  • 3. View Composers.

1. Creating Views.

Views contain the HTML derved by your application and seperated your controller/application logic from your presentation logic. A simple view may be look like this:-

Views must be save as the extension .blade.php.

Views part:-

<body>
        <h1>Hello, {{ $name }}</h1>
    </body>

Route Path:-

Route::get('index', function () {
    return view('greeting', ['name' => 'Sonu']);
});

You can see the first argument pass to the view helper corresponding to the name of the view file. The second argument is the array of data that should be display on the view part.

Determining If A View Exists

If you have to need to determine if a view exists, you can use the view facade. The exists method will return true if the view exists.

use Illuminate\Support\Facades\View;

if (View::exists('emails.customer')) 
{
   return true;
}

2. Passing Data To Views.

As you notice in previous example. We passed an array of data to views:-

return view('greetings', ['name' => 'Sonu']);

When we passing data in this format. Then you can acces like this:-

<?php echo $name;?>

If you are passing a complete array of data to the view helper function, you may use the with function.

return view('greeting')->with('name', 'Sonu');

Sharing Data With All Views

We already seen how we can pass data to view, if we have to need to pass data to all the views. Then we called share function. share() method will take two argument, key and values. It is called from boot method of service provider.

Example:-

Route Path:-

Route::get('index', function(){
   return view('index');
});
Route::get('index1', function(){
   return view('index1');
});

Create two view file in views directory:-index.blade.php and index1.blade.php with same code.

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

AppServiceProvider- Here we have used share methed and the data that we have passed will be shared with all the views(app/Providers/AppServiceProvider.php)

Let’s look at a simple example.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

   public function boot(){
      view()->share('name', 'Tutorialsplane.com');
   }

   public function register(){
      //
   }
}
Laravel Views

3. View Composers.

Views composers are callback or class method. We called this function when a view is loaded. If you have data that you want to be bound to a view each time that view is rendered. It can help you to organize that logic into a single location.

Let’s look at a simple example.

<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
  
    public function boot()
    {
       
        View::composer(
            'profile', 'App\Http\ViewComposers\ProfileComposer'
        );
       
        View::composer('dashboard', function ($view) {
           
        });
    }

    public function register()
    {
        //
    }
}

Now we have registered the composer, the ProfileComposer@compose method will be run each time at the profile view is being rendered.

Let's look at a simple example.

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Repositories\UserRepository;

class ProfileComposer
{
    protected $users;

    public function __construct(UserRepository $users)
    {
       
        $this->users = $users;
    }

    public function compose(View $view)
    {
        $view->with('count', $this->users->count());
    }
}

Attaching A Composer To Multiple Views

By using composer method we can attach muliple view at once by passing an array of views as first argument.

View::composer(
    ['profile', 'dashboard'],
    'App\Http\ViewComposers\MyViewComposer'
);

The composer method also accept the * character to allowing you to attach a composer to all views.

View::composer('*', function ($view) {
    //
});

View Creators

View creater are quite similar to view composer. They are executed immediately. To register a view creater, use the creater method.

View::creator('profile', 'App\Http\ViewCreators\ProfileCreator');

Advertisements

Add Comment

📖 Read More