Laravel Data Views

Passing the Data Views

The data should be an array with the key/value pairs while passing the information in the following (return view(‘greeting’, [‘name’ => ‘John’]);) manner.

Inside the view, we can access each value by using its corresponding key easily, like <?php echo $key;?>

To pass a complete array of data to the view helper function as an alternative, we use the with() method for adding an individual piece to the views:

return view(‘greeting’, [‘name’ => ‘John’]);

Sharing Data with All Views

We need to share a piece of data with all kind of views that are provided by our application. We use the view façade's share method.

We should place calls to share it within a service provider`s boot method. We are free to add them to the AppServiceProvider or generate a separate service provider to them:

<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
/**
     * Register any application services.
     *
     * @return void
     */
public function register()
    {
        //
    }
/**
     * Bootstrap any application
services.
     *
     * @return void
     */
public function boot()
    {
        View::share('key', 'value');
    }
}

View Composers:

View composers are callbacks or class methods that are called when a view is provided.

 If we have data that we want to drive from a view each time, which is provided. A view composer can help us to organize logic into a single location.

For example, let`s register the view composers within a serviceprovider.

We will use the view façade to access the underlying Illuminate\Contracts\View\Factory contract implementation.

We have to remember that Laravel does not include a default directory for view composers. We are free to organize them; however, we wish.

We could create an App/Http/View/Composers directory:

<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
     *
  *
      * @return void
      */
 public function register()
     {
         //
 }
 /**
      *     
      * @return void
      */
 public function boot()
     {
 // Using class-based composers.
         View::composer(
             'profile', 'App\Http\View\Composers\ProfileComposer'
         );
         // Using Closure based composers...
         View::composer('dashboard', function ($view) {
             //
         });
     }
 } 

    Note:

If we want to create a new service provider that contains our view composer registrations, we will need to add the service provider to the provider's array in the config/app.phpconfiguration file.

Now, we have registered the composer; the [email protected] method will be executed each time, the profile view is being provided.

Let's define the composer class: 

<?php
namespace App\Http\View\Composers;
use Illuminate\View\View;
use App\Repositories\UserRepository;
class ProfileComposer
{
/**
     * The user repository
implementation.
     *
     * @var UserRepository
     */
protected $users;
/**
     * Create a new profile composer.
     *
     * @param  UserRepository  $users
     * @return void
     */
public function __construct(UserRepository $users)
    {
        // Dependencies automatically
resolved by service container...
        $this->users = $users;
    }
/**
     * Bind data to the view.
     *
     * @param  View 
$view
     * @return void
     */
public function compose(View $view)
    {
        $view->with('count',
$this->users->count());
    }
}

Just before the view is provided, the composer's compose method is called with the Illuminate\View\View instance. We use with() method to bind data to the view.

  • All the view composers are resolved via the service container. We may type-hint any dependencies that we need within a composer's constructor.

Attaching a Composer to Multiple Views:

We attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:

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

The composer method also accepts the * character as a wildcard, it allow us to attach a composer to all views:

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

View Creators:

View creators are similar to view composers. They are executed immediately after the view is personalized of waiting, until the views are about to make. To register a view creator, we use the creator method:

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



ADVERTISEMENT
ADVERTISEMENT