承接 laravel-admin/crud 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

laravel-admin/crud

Composer 安装命令:

composer require laravel-admin/crud

包简介

Crud Package for Laravel Admin

README 文档

README

This package is experimental, don't use it for production.

Installation

Install from Packagist:

composer require laravel-admin/crud

Add the service provider to your app.php config file

LaravelAdmin\Crud\CrudServiceProvider::class,

Usage

First create a migration, model and admin controller for your module. Add the admin controller as a resource to your routes, like:

Route::resource('blog', 'BlogController');

Resource controller

Setup your basic controller for crud methods as follow:

<?php

namespace App\Http\Controllers;

use LaravelAdmin\Crud\Controllers\ResourceController;

use App\Blog;

class BlogController extends ResourceController
{
  protected $model = Blog::class;

  protected $singular_name = "blog";
  protected $plural_name = "blogs";

}

The ResourceController includes all the crud methods for a Laravel resource controller, like index, create, store, edit, update and destroy. At this point only the show method will not be used.

The view who will be rendered by index,create and edit are default bootstrap and compatible with the views which will be scaffoled by artisan make:auth.

The only thing you have to do is defining your fields and validation. Herefore you can override a couple of methods of the parent controller in your own controller.

Validation on store

protected function getValidationRulesOnStore()
{
        return [
                'title' => 'required|string|min:6',
                'body'  => 'required|string',
        ];
 }

Defining your fields for the create form

protected function getFieldsForCreate()
{
   return [
     [
       'id' => 'title',
       'label' => 'Title',
       'description' => 'This is the main title of the page',
       'field' => 'text',
     ],
     [
       'id' => 'body',
       'label' => 'Body',
       'field' => 'textarea',
     ]
  ];
}

Manipulate your payload for store

This method is optional, by default the payload will be the fields which are defined in the getFieldsForCreate method. If you want to manipulate your data, like a password or date format you can implement this method

protected function getPayloadOnStore(array $data)
{
  $payload = $this->getPayloadForStoreDefault($data);

  //    If password is given, lets encrypt it, otherwise remove the password from the payload
  if (!empty($payload['password'])) $payload['password'] = bcrypt($payload['password']);
  else unset($payload['password']);

  return $payload
}

Edit model

For editing your model, the same methods as store are available:

  • getValidationRulesOnUpdate
  • getFieldsForEdit
  • getPayloadOnUpdate

Note: If your settings for store and update are the same, you only have to define the methods for the update.

View your records

For generating the index view, you can define your fields with the getFieldsForList method.

protected function getFieldsForList()
{
   return [
     [
       'id' => 'title',
       'label' => 'Title',
     ],
     [
       'id' => 'created_at',
       'label' => 'Created',
       'formatter' => function($model)
        {
          return $model->created_at->format('Y-m-d');
        }
     ]
  ];
}

Note: Each field has an optional formatter property. By default the property of the model defined in the id is shown. You can assign a accessor in the format field as string, or a callback as shown above.

More to come....

Layout module

This package includes a layout builder for your page.

Installation

Add the layout config file to your local config folder.

php artisan vendor:publish --provider="LaravelAdmin\Crud\CrudServiceProvider::class"

Add the Layout vue component to your view instance

import layout from '../../../vendor/laravel-admin/crud/resources/js/components/layout/Layout.vue';
Vue.component('layout', layout);

Build your components

Within the config file you can build your own components. Default one simple component is included in the config.

Each component is a combination of fields. The type of fields are:

  • Textield (layout-text)
  • Textarea (layout-textarea)
  • Boolean (layout-boolean)
  • Selectbox (layout-select)
  • Wysiwyg (layout-wysiwyg) (TinyMCE)
  • Media item (layout-media-item) (Only available if LaravelAdmin/MediaManager is available)

note: The selectbox field can have an options attribute, this can be an array or a callback.

Component drivers

Each component can have a custom driver when you need some extra logic before rendering it into a view.

Add a full namespaced class into the driver attribute of your component config to enable it.

The best practice is to extends the default component class:

\LaravelAdmin\Crud\Layout\Component

The following methods can be extended or overwritten:

  • getContent
  • getView
  • isActive
  • render

Field drivers

Also each field can have a custom driver, this can be useful when the content from the admin has to be formatted before sending it to the view.

The best practice is to extends the default field class:

\LaravelAdmin\Crud\Layout\Field

You can add methods or overwrite the toString method.

Views

Create your view in the folder which is defined in the layout config (default 'layout'). The name of the template is the same of the id of your component, like 'basic-text.blade.php'.

Note: If you use your own driver, you van set a custom view within the method 'getView'.

Create your admin controller

Within your module add a LayoutController file like this:

<?php

namespace App\Http\Controllers\Admin\Pages;

use LaravelAdmin\Crud\Controllers\LayoutController as BaseController;

class LayoutController extends BaseController
{
    use Shared;

    protected $model = \App\Models\Page::class;
    protected $instance;

    protected $singular_name = 'page';
    protected $plural_name = 'pages';
}

Add the following route into your module routes:

Route::resource('pages.layout', 'Admin\Pages\LayoutController');

If your layout is in the translatable table, the route will be:

/admin/pages/1/layout/en

If not, it will be:

/admin/pages/1/layout

Render the layout

Add the following trait to the model which has a layout field:

\LaravelAdmin\Crud\Traits\HasLayout

There a several ways to render your layout. The most easy solutions is to render it directly from your view file:

{!! $model->layout()->render() !!}

If you want to use blade includes:

@foreach ($model->layout()->components() as $component)
	@include($component->getView(), $component->getContent())
@endforeach

If you want to have executed the logic in your controller:

//	Controller
public function show($id)
{
	$model = \App\Models\Page::findOrFail($id);
	$layout = $model->layout()->components();

	return view('page', compact('model','layout'));
}

// View
{!! $layout->render() !!}

laravel-admin/crud 适用场景与选型建议

laravel-admin/crud 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.16k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-28