定制 sfneal/view-models 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sfneal/view-models

最新稳定版本:5.2.0

Composer 安装命令:

composer require sfneal/view-models

包简介

spatie/view-models wrapper with built in response caching.

README 文档

README

Packagist PHP support Latest Version on Packagist Build Status StyleCI Scrutinizer Code Quality Total Downloads

spatie/view-models wrapper with built in response caching

Installation

You can install the package via composer:

composer require sfneal/view-models

Usage

A view model is a class where you can put some complex logic for your views. This will make your controllers a bit lighter. You can create a view model by extending the provided Sfneal\ViewModels\AbstractViewModel.

use Sfneal\ViewModels\ViewModel;

class PostViewModel extends ViewModel
{
    public $indexUrl = null;
   

    public function __construct(User $user, Post $post = null)
    {
        $this->user = $user;
        $this->post = $post;
        
        $this->indexUrl = action([PostsController::class, 'index']);
        $this->view = 'your.view'; 
    }
    
    public function post(): Post
    {
        return $this->post ?? new Post();
    }
    
    public function categories(): Collection
    {
        return Category::canBeUsedBy($this->user)->get();
    }
}

And used in controllers like so:

class PostsController
{
    public function create()
    {
        // Uses caching for fast load times after first request
        return (new PostViewModel(current_user()))->render();
    }
    
    public function edit(Post $post)
    {
        // Doesn't use caching to avoid need for cache invalidation on changes
        return (new PostViewModel(current_user(), $post))->renderNoCache();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email stephen.neal14@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.

统计信息

  • 总下载量: 91.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固