在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):laravel-auto-presenter/laravel-auto-presenter开源软件地址(OpenSource Url):https://github.com/laravel-auto-presenter/laravel-auto-presenter开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Laravel Auto Presenter 7This package automatically decorates objects bound to views during the view render process. Features
InstallingLaravel Auto Presenter requires PHP 7.1-8.1 and supports Laravel 5.5-9. If you need support for older Laravel or PHP versions, please choose an older version of Laravel Auto Presenter.
To get the latest version, simply require the project using Composer: $ composer require mccool/laravel-auto-presenter Once installed, if you are not using automatic package discovery, then you need to register the You can also optionally alias our facade: 'AutoPresenter' => McCool\LaravelAutoPresenter\Facades\AutoPresenter::class, UpgradingVersion 6 to 7In Laravel Auto Presenter 7, note that:
Version 5 to 6Going from Laravel Auto Presenter 5, to 6, note that:
Version 4 to 5If you're upgrading from Laravel Auto Presenter 4, to 5, note that:
UsageTo show how it's used, we'll pretend that we have an Eloquent Post model. It doesn't have to be Eloquent, it could be any kind of class. But, this is a normal situation. The Post model represents a blog post. I'm using really basic code examples here, so just focus on how the auto-presenter is used and ignore the rest. use Example\Accounts\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = 'posts';
protected $fillable = ['author_id', 'title', 'content', 'published_at'];
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
} Also, we'll need a controller.. use Example\Accounts\Post;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
class PostsController extends Controller
{
public function getIndex()
{
$posts = Post::all();
return View::make('posts.index', compact('posts'));
}
} and a view... @foreach($posts as $post)
<li>{{ $post->title }} - {{ $post->published_at }}</li>
@endforeach In this example the published_at attribute is likely to be in the format: "Y-m-d H:i:s" or "2013-08-10 10:20:13". In the real world this is not what we want in our view. So, let's make a presenter that lets us change how the data from the Post class is rendered within the view. use Carbon\Carbon;
use Example\Accounts\Post;
use McCool\LaravelAutoPresenter\BasePresenter;
class PostPresenter extends BasePresenter
{
public function published_at()
{
$published = $this->wrappedObject->published_at;
return Carbon::createFromFormat('Y-m-d H:i:s', $published)
->toFormattedDateString();
}
} Note that the model is injected by calling the We need the post class to implement the interface. use Example\Accounts\User;
use Example\Blog\PostPresenter;
use McCool\LaravelAutoPresenter\HasPresenter;
use Illuminate\Database\Eloquent\Model;
class Post extends Model implements HasPresenter
{
protected $table = 'posts';
protected $fillable = ['author_id', 'title', 'content', 'published_at'];
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
public function getPresenterClass()
{
return PostPresenter::class;
}
} Now, with no additional changes our view will show the date in the desired format. The TroubleshootingIf an object isn't being decorated correctly in the view then there's a good chance that it's simply not in existence when the view begins to render. For example, lazily-loaded relationships won't be decorated. You can fix this by eager-loading them instead. Auth::user() will never be decorated. I prefer to bind $currentUser to my views, anyway. If an object is a relation of another object and it isn't being decorated in the view, you might not have added the SecurityIf you discover a security vulnerability within this package, please send an email to [email protected]. All security vulnerabilities will be promptly addressed. You may view our full security policy here. LicenseLaravel Auto Presenter is licensed under The MIT License (MIT). For EnterpriseAvailable as part of the Tidelift Subscription The maintainers of |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论