tychovbh/laravel-mvc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tychovbh/laravel-mvc

Composer 安装命令:

composer require tychovbh/laravel-mvc

包简介

Add mvc structure to laravel

README 文档

README

Latest Version on Packagist Software License Total Downloads

Laravel MVC is created by, and is maintained by Tycho, and is a Laravel/Lumen package to manage all your data via a Repository. Feel free to check out the change log, releases, license, and contribution guidelines

Install

Via Composer

$ composer require tychovbh/laravel-mvc

For lumen application add Service Provider to bootstrap/app.php

$app->register(\Tychovbh\Mvc\MvcServiceProvider::class);

Usage

Repositories

Create a Repository:

// Creates a repository in app/Repositories
artisan mvc:repository UserRepository

Use The UserRepository in controller, but you can use it anywhere else too.

class UserController extends AbstractController
{
    public function __construct(UserRepository $repository)
    {
        $this->repository = $repository
    }
    
    public function index()
    {
        $users = $this->repository->all();
        return response()->json($users)
    }
}

Make sure you have a Model that your repository can use. If you want to use save/update methods add $filled to your model.

class User extends model
{
    protected $filled = ['name']
}

Available methods"

// Get all
$this->repository->all();

// Search all resources where name: Jan
$this->repository::withParams(['name' => 'Jan'])->get();

// Search all resources where name: Jan and get first.
$this->repository::withParams(['name' => 'Jan'])->first();

// Search all resources where names in: Jan and piet
$this->repository::withParams(['name' => ['jan', 'piet']])->get();

// Order all resources by name or any other Laravel statement
$this->repository::withParams(['sort' => 'name desc')]->get();

// Paginate 10
$this->repository->paginate(10);

// Paginate 10 where country in Netherlands or Belgium.
$this->repository::withParams(['country' => ['Netherlands', 'Belgium']])->paginate(4);

// Search resource with ID: 1
$this->repository->find(1);

// Store resource in the database. This uses laravel fill make sure you add protected $filled = ['name'] to your User model.
$user = $this->repository->save(['name' => 'jan']);

// Update resource.
$user = $this->repository->update(['name' => 'piet'], 1);

// Destroy resource(s).
$this->repository->destroy([1]);

If you wish to override on of the methods above just add it to you repository

class UserRepository extends AbstractRepository implements Repository
{
    public function find(int $id)
    {
        // add your own implementation of find
        return $user;
    }
    
    public function save($data)
    {
        // Add some logic and then call parent save
        $data['password'] = Hash:make($data['password']);
        return parent::save($data);
    }
    
    // You can add your own custom params to filter the request
    // This will be triggered when key is "search" is added to the params:
    // Let's say we want to build a search on firstname, lastname and email:
    // $repository->params(['search' => 'jan'])->all();
    // $repository->params(['search' => 'jan@gmail.com'])->all();
    // $repository->params(['search' => 'piet'])->all();
    // We can do that by adding a method, just capitalize the param key and add index{key}Param to the method name.
    public function indexSearchParam(string $search)
    {
        $this->query->where('email', $search)
                    ->orWhere('firstname', $search)
                    ->orWhere('surname', $search);    
    }
    
    // You can do the same for show methods like find
    public function showSearchParam(string $search);
}

Controllers

Create a Controller:

// Creates a Controller in app/Http/Controllers
artisan mvc:controller UserController

All Laravel Resource methods are now available (index, show, store, update, destroy). See their documentation here for setting up routes: laravel resource controllers.

You can override Resource methods to do project related stuff

class UserController extends AbstractController
{
    public function index()
    {
        // Do stuff before querying
        
        $response = parent::index();

        // Do stuff after querying

        return $response;
    }
}

Form Requests

Create a Form Request:

// Creates a Form Request in app/Http/Requests
artisan mvc:request StoreUser

You can use the request middleware "valdiate" to validate the request. It will look for the FormRequest and validate it So for example model User:

  • store request (POST /users) will look for a FormRequest with name StoreUser
  • update request (UPDATE /user/{id}) will look for a FormRequest with name UpdateUser
// routes/web.php (Laravel)
$router->post('/users', 'UserController@index')
->name('user.index')
->middleware('validate');

// routes/web.php (Lumen)
$router->post('/users', [
    'middleware' => 'validate',
    'as' => 'users.index',
    'uses' => 'UserController@index'
]);

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email info@bespokeweb.nl instead of using the issue tracker.

Credits

License

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

tychovbh/laravel-mvc 适用场景与选型建议

tychovbh/laravel-mvc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.9k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tychovbh」 「laravel-mvc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tychovbh/laravel-mvc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tychovbh/laravel-mvc 我们能提供哪些服务?
定制开发 / 二次开发

基于 tychovbh/laravel-mvc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-08