tychovbh/laravel-mvc

为Laravel添加MVC结构

v3.3 2021-01-26 09:55 UTC

README

Latest Version on Packagist Software License Total Downloads

Laravel MVC是由Tycho创建和维护的,是一个用于通过仓库管理所有数据的Laravel/Lumen包。您可以自由地查看变更日志发布版本许可证贡献指南

安装

通过Composer

$ composer require tychovbh/laravel-mvc

对于Lumen应用程序,将服务提供者添加到bootstrap/app.php

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

用法

仓库

创建一个仓库

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

在控制器中使用UserRepository,但您也可以在其他任何地方使用它。

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

确保您有一个模型,该模型可以被仓库使用。如果您想使用保存/更新方法,请在模型中添加$filled。

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

可用方法

// 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]);

如果您想覆盖上述方法之一,只需将其添加到您的仓库中。

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);
}

控制器

创建一个控制器

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

现在所有Laravel资源方法都可用(index、show、store、update、destroy)。有关设置路由的文档,请参阅Laravel资源控制器

您可以覆盖资源方法以执行项目相关操作。

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

        // Do stuff after querying

        return $response;
    }
}

表单请求

创建一个表单请求

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

您可以使用请求中间件"validate"来验证请求。它将查找FormRequest并验证它,例如,对于User模型

  • 存储请求(POST /users)将查找名为StoreUser的FormRequest
  • 更新请求(UPDATE /user/{id})将查找名为UpdateUser的FormRequest
// 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'
]);

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCODE_OF_CONDUCT以获取详细信息。

安全性

如果您发现任何安全问题,请通过info@bespokeweb.nl发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件