Package Data | |
---|---|
Maintainer Username: | slashequip |
Maintainer Contact: | sam@haganjones.com (Sam Jones) |
Package Create Date: | 2017-04-04 |
Package Last Update: | 2018-01-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:01:52 |
Package Statistics | |
---|---|
Total Downloads: | 117 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 9 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Like Laravel's Mailables, Viewables allow you to take a class based approach to working with and handling your views in Laravel.
composer require "haganjones/laravel-viewables"
In config/app.php
add the below to your service providers array:
HaganJones\LaravelViewables\Providers\ServiceProvider::class,
All of a viewable class' configuration is done in the build
method.
Within this method, you may call various methods such as view
and
with
to render and pass data to the view.
Within a viewable class' build
method, you may use the view
method
to specify which template should be used when rendering the view:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('dashboard');
}
Typically, you will want to pass some data to your view that you can utilize when rendering the view's HTML. There are two ways you may make data available to your view. First, any public property defined on your viewable class will automatically be made available to the view. So, for example, you may pass data into your viewable class' constructor and set that data to public properties defined on the class:
<?php
namespace App\View;
use App\User;
use HaganJones/LaravelViewables/View/Viewable;
class DashboardView extends Viewable
{
/**
* The user instance.
*
* @var User
*/
public $user;
/**
* Create a new viewable instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the view.
*
* @return $this
*/
public function build()
{
return $this->view('dashboard');
}
}
Once the data has been set to a public property, it will automatically be available in your view, so you may access it like you would access any other data in your Blade templates:
<div>
Logged in as: {{ $user->name }}
</div>
with
Method:If you would like to customize the format of your view's data before it is
sent to the template, you may manually pass your data to the view via
the with
method. Typically, you will still pass data via the
viewable class' constructor; however, you should set this
data to protected
or private
properties so the data
is not automatically made available to the template.
Then, when calling the with
method, pass an
array of data that you wish to make available
to the template:
<?php
namespace App\View;
use App\User;
use HaganJones/LaravelViewables/View/Viewable;
class DashboardView extends Viewable
{
/**
* The user instance.
*
* @var User
*/
protected $user;
/**
* Create a new viewable instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('dashboard')
->with([
'userName' => $this->user->name,
'userEmail' => $this->user->email,
]);
}
}
Once the data has been passed to the with
method, it will automatically be available
in your view, so you may access it like you would access any other data in your
Blade templates:
<div>
Logged in as: {{ $userName }}
</div>
~~To serve a view, use the helper function viewable()
. You may pass an
instance of your viewable class to this helper function:~~
As of release 0.2 Viewables implement Laravel's Responsable Interface which
now allows you to return a new instance of a Viewable Class straight from
the controller. The viewable()
function is still available but will be
removed in a future release.
<?php
namespace App\Http\Controllers;
use App\User;
use App\View\DashboardView;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class DashboardController extends Controller
{
/**
* Show the dashboard page
*
* @return Response
*/
public function get()
{
//return viewable(
// new DashboardView(Auth::user())
//);
return new DashboardView(Auth::user());
}
}