pion/laravel-repository-controllers

v0.9.3 2016-06-21 16:45 UTC

This package is auto-updated.

Last update: 2024-09-21 19:27:21 UTC


README

一些代码行,通过仓库方式创建表单/创建页面。默认情况下,控制器支持所有CRUD方法(缺少索引/显示页面,您可以在本地实现)。控制器使用包含模型所有标准方法的仓库类,只需为您的Eloquent模型创建一个仓库类,返回该类即可。

更改

0.9.3

  • 修复了prepareCreateFormData使用时未传递正确请求对象的问题
  • 仓库特质支持createNavigationForCreateAction和createNavigationForEditAction
  • 更新了RepositoryController以使用抽象

0.9.2

  • 移除了crazycodr/standard-exceptions依赖

0.9.1

  • 两个仓库控制器中的getFormView现在有新的带有当前请求的参数
  • 所有prepareFormData方法现在有新的带有当前请求的参数(最后一个属性)
  • ChildRepositoryController特质现在有请求参数,然后是父对象

需求

Laravel Framework (5.1, 5.2) - 5.0 not tested

安装

composer require pion/laravel-repository-controllers

将服务提供程序添加到app.php配置中,以复制翻译

Pion\Repository\RepositoryServiceProvider::class

运行发布命令以复制翻译(Laravel 5.2)

php artisan publish --provider="Pion\Repository\RepositoryServiceProvider"

运行发布命令以复制翻译(Laravel 5.1)

php artisan vendor:publish --provider="Pion\Repository\RepositoryServiceProvider"

用法

  • 对于没有任何父关系的模型(根表/模型)使用RepositoryControllerTrait
  • 对于属于不同模型的模型,可以使用ChildRepositoryControllerTrait(您还必须为父模型创建控制器)
  • 您必须包含CrumbsNavigationTrait(对于自定义提供程序,请使用NavigationTrait或从pion/laravel-support-controllers分叉以实现喜欢的包),否则逻辑会崩溃。
  • 导航特质可以添加到基本控制器中。

RepositoryControllerTrait

在控制器的构造函数中使用bootRepository()来准备仓库数据。这将还会使用翻译的标题等。最佳实践是构建自己的使用该特质的RepositoryController。然后您可以从特质中扩展方法,就像扩展普通类一样。对于默认用法,您可以扩展提供的RepositoryController

/**
 * RepositoryController constructor.
 */
public function __construct()
{
    parent::__construct();
    // required!
    $this->bootRepository();
    $this->formView = "admin.season.form"; // the desired form
    
    // optional
    $this->editTitle = "Editing this entry"; // call after boot!
    $this->formObjectIndex = "season";  // the index of the object in form
    $this->detailModelAction = "edit"; // instead of showing the detail action to show, use the edit page
    $this->setRedirectToEditOnCreate(true);
}

或者您可以

/**
 * RepositoryController constructor.
 */
public function __construct()
{
    parent::__construct();
    $this->bootRepositoryWith("admin.season.form", "Create new!", "Edit this one");
}

实现createRepository方法。您可以选择返回仓库

/**
 * Creates a AreaRepository repository
 * @return AreaRepository
 */
protected function createRepository()
{
    return new AreaRepository(); // this will use the RepositoryTrait
}

您可以通过扩展这些函数来覆盖编辑/创建的表单数据以及这两种用法。

/**
 * Prepares the form data for all the views
 * @param array $data
 * @param Model|null $object
 * @param Request $request
 */
protected function prepareFormData(array &$data, $object, $request) {}

/**
 * Prepares the data only for the
 * @param array $data
 * @param Model $object
 * @param Request $request
 */
protected function prepareEditFormData(array &$data, $object, $request) {}

/**
 * Prepares the data for create form
 * @param array $data
 * @param Request $request
 */
protected function prepareCreateFormData(array &$data, $request) {}

导航定制

自0.9.3版本起支持

您可以通过方法自定义自己的导航(您可以选择是否调用特质子类的父方法)

/**
 * Generates the navigation for create action
 * @param Request $request
 */
protected function createNavigationForCreateAction(Request $request)
{
    $this->createNavigation($this->createTitle);
}

/**
 * Generates the navigaiton for edit action
 * @param Request $request
 * @param Model $object
 */
protected function createNavigationForEditAction(Request $request, $object)
{
    $this->createNavigation($this->editTitle, $object);
}

ChildRepositoryControllerTrait

与仓库控制器的引导用法非常相似,但具有更多选项

/**
 * Defines the index for the parent object in the form view. Must be in the
 * data of the model (witout the _id)
 * @var string
 */
protected $formParentObjectIndex = "parent";
/**
 * Defines a list of collumns we want to select from the parent. If empty
 * the repository will use the name collumn and the key
 * @var array
 */
protected $parentObjectSelect = [];
/**
 * Defines the collumn for the parent object name
 * @var string
 */
protected $parentObjectNameCollumn = "name";

您可以通过扩展这些函数来覆盖编辑/创建的表单数据以及这两种用法。

/**
 * Prepares the form data for all the views
 * @param array $data
 * @param Model|null $object
 * @param Model $parentObject
 */
protected function prepareFormData(array &$data, $object, $parentObject) {}

/**
 * Prepares the data only for the
 * @param array $data
 * @param Model $object
 * @param Request $request
 * @param Model $parentObject
 */
protected function prepareEditFormData(array &$data, $object, $request, $parentObject) {}

/**
 * Prepares the data for create form
 * @param array $data
 * @param Request $request
 * @param Model $parentObject
 */
protected function prepareCreateFormData(array &$data, $request, $parentObject) {}

RepositoryTrait

将处理创建/更新/销毁以及所有表单验证的仓库实例。您可以为BaseRepository创建自己的使用RepositoryTrait的实例,所有仓库都将扩展BaseRepository。

对于复选框验证,您可以添加要验证的复选框索引列表,并将为未选中/选中状态预填充0或1。未选中的复选框将添加到请求中,值为0。

必须实现

getClass

您必须返回模型类

/**
 * @return string
 */
protected function getClass()
{
    return Area::class;
}

getValidationRules($isNew, array $data, $model = null)

一组验证规则。您可以返回空数组。

protected function getValidationRules($isNew, array $data, $model = null)
{
    return [
        "name" => "required|string|max:255",
        "position" => "required|numeric"
    ];
}

通过调用"父"逻辑扩展资源方法(或特质)

您必须使用所需的特质,并更改您想要扩展的特质方法。然后您可以使用自己的方法调用该方法。

use ChildRepositoryControllerTrait {
    store as baseStore;
}
    
/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request $request
 * @param int $parentId
 * @return \Illuminate\Http\Response
 */
public function store(Request $request, $parentId)
{
    // redirect to new form with old data so we can create same properties easier
    return $this->baseStore($request, $parentId)->withInput($request->except([
        "name", "position", "identifier"
    ]));
}

待办事项

  • 更新readme
  • 添加示例
  • 是否要分离导航的使用?