radfic/gastropod

为 Laravel 8 的最小化 CRUD 生成器

v0.0.1 2022-02-21 12:46 UTC

This package is auto-updated.

Last update: 2024-09-25 04:00:30 UTC


README

请注意,GASTROPOD 目前仍处于预 Alpha 阶段,仅用于实验或您想要贡献但不要用于生产环境。

Gastropod 是一个简单的 Laravel 包,灵感来源于 Grocery Crud 包,用于 Code Igniter,旨在加快并简化小型网站基于 CRUD 的管理页面的创建。它假设您已经有一个用户表和一个 User Eloquent 模型,可以自动创建简单的用户和 Admin CRUD。您可以进一步扩展它来管理所有表,并且可以通过非常少的代码设置一个基本且可扩展的 CRUD 系统。Gastropod 视图使用 BootstrapjQuery 创建,并且它会从 CDN 中拉取所需的脚本和 CSS,而无需您做任何事情。它的身份验证基于现有的 Laravel 身份验证系统,只需添加一个表来引用哪些用户可以访问 CRUD。但用户仍然会通过您的用户表登录,而无需对其进行修改。

1) 使用 composer 安装

您可以使用 composer 安装此包

composer require radfic/gastropod

2) 运行 artisan 安装脚本

之后,您需要运行 gastropod:install artisan 命令

php artisan gastropod:install

它将在您的应用程序目录结构中发布多个文件

--App
  --Models
    --GastropodAdmin.php //It's the default model which Gastropod will use to authenticate users.
--config
  --gastropod.php //Will hold all configuration parameters of Gastropod.
--database
  --migrations
    --2022_02_14_000001_create_gastropod_admins_table.php //a migration which will create the gastropod_admins table in your db.
--public
  --gastropod_assets //will contain all required Gastropod assets: images, css, js and such.
--resources
  --views
    -gastropod //will contain all Gastropod related views.
--routes
  --gastropod.php //a routes file to hols all Gastropod related routes.

3) 运行迁移

发布您的资产后,您的应用程序的迁移文件夹中会新增一个迁移文件: 2022_02_13_172741_create_gastropod_admins_table.php。它定义了数据库中的一个新表,以保存允许访问 gastropod 的用户引用。

3) 现在,您应该运行迁移以让 artisan 为您创建表,通过运行 artisan migrate 命令

php artisan migrate

运行迁移后,您应该在数据库中有一个新表:'gastropod_admins'。

|----------------------------------------|
| gastropod_admins                       |
|----------------------------------------|
| id | user_id | created_at | updated_at |
|----|---------|------------|------------|

Gastropod 假设您已经在应用程序中有一个用户表和一个 User 模型。为了让用户使用 Gastropod,您必须在这个表中为每个用户添加一条记录,引用用户的 id。第一个 Gastropod 管理员必须通过您自己的方式设置(例如,使用 PhpMyAdmin)。Gastropod 安装后,您将使用其界面添加更多管理员。

3.1) 创建第一个管理员

手动添加一个管理员,在 gastropod_admins 中插入一条新记录,引用用户表中的行

INSERT INTO `gastropod_admins` (`user_id`) VALUES (USER-ID-TO-MAKE-ADMIN);

此用户现在将被允许登录到 gastropod。您希望赋予访问 Gastropod 的任何其他用户都应该在此表中有一个相关记录。

4) 在 RouteServiceProvider 中注册 Gastropod 路由

最后一步是将 gastropod 路由注册到您的应用程序的 RouteServiceProvider 中。为此,打开文件 app\Providers\RouteServiceProvider.php,并在 boot 函数的所有其他条目之后添加 gastropod 部分

public function boot()
{
    $this->routes(function () {
    
      /** [... YOUR OTHER ROUTE GROUPS: web, api, ecc ..] */
      
      //G@STROPOD ROUTES->copy in your RouteServiceProvider
      Route::prefix('gastropod')
          ->middleware('web')
          ->namespace($this->namespace)
          ->group(base_path('routes/gastropod.php'));
      //END of G@STROPOD ROUTES
      
    });
}

完成:检查您的安装!

前往 /gastropod 路由以查看登录页面是否显示。如果显示了,您应该使用与 您之前在 gastropod_admins 表中插入的记录 相关的用户登录。如果一切顺利,现在您应该能看到您的用户表。同时,应该已经设置了一个可通过菜单访问的 gastropod_admins 表。

创建您的第一个 gastropod crud

如果您的 Gastropod 运行正常,接下来您可能想知道如何将模型添加到 crud 中。让我们从一个真实场景的例子开始:您想添加一个 users 表。Gastropod 假设您在尝试创建新的 crud 之前已经设置了所有表格和模型,并定义了所有相关关系,所以让我们假设我们已经有了一个表格和一个模型:一个 users 表和一个 User 模型。让我们从使用自定义 artisan make:gastropodController 命令开始。它需要两个参数

  • 您要创建的控制器名称,对于用户控制器,我建议如下:Gastropod\GastropodUserController。请注意,这样控制器类文件将在您的 app/Http/Controllers 文件夹下的 Gastropod 文件夹中创建。
  • 您要 crud 的模型名称。在这个例子中,我们想要 crud User 模型。
php artisan make:gastropodController Gastropod\GastropodUserController User

之后,您将在您的应用中有一个全新的 Gastropod 控制器。检查一下,因为您可能想要修改它

<?php

namespace App\Http\Controllers\Gastropod;
use RadFic\Gastropod\Http\Controllers\GastropodCrudController;

/** We need to import the models we will need later on. */
use App\Models\User;

/**
 * GastropodUserController
 * 
 * It must extend RadFic\Gastropod\Http\Controllers\GastropodCrudController.
 */
class GastropodUserController extends GastropodCrudController
{
	/**
	 * In the constructor we do all needed configuration for the resource crud.
	 */
    public function __construct()
    {
        /**
         * The Eloquent model we want to crud.
         */
        $model = User::class;
        
		/**
		 * Relations map is a map of all relations we want our crud to take care of.
		 * `key` is the name of the field holding reference to the other class id.
		 * `field` is the name of the field we want to show in our crud from the related table.
		 * `model` is the Eloquent model of the referenced table.
		 */
        $relationsMap = [
			/**
			 * We define this default relationship using gastropod_admins 
			 * table's `user` relationship.
			 */	

			 /* Example:
			 'profile' => [
                'key' => "profile_id",
                'field' => 'name',
                'model' => Profile::class  //always remember to import the other model as well, using `use`
             ]
			*/		
        ];

		/**
		 * After setup we call GastropodCrudController's constructor 
		 * to take care of the init job.
		 */
        parent::__construct($model,$relationsMap);
    }
}

由于我们的 User 模型没有需要考虑的关系,我们的控制器已经就绪。现在我们只需要在我们的 routes/gastropod.php 文件中添加资源

  /** resource routes */
  Route::resources([
    /**
    * gastropod_admins is installed by default: it manages the crud admin permissions on app users. 
    */
    'gastropod_admins' => 'RadFic\Gastropod\Http\Controllers\GastropodAdminController',
    /** Our brand new resource: Users */
    'users' => 'App\Http\Controllers\Gastropod\GastropodUserController',
  ]);

在那里,我们将在默认的 gastropod_admins 条目之后添加一个 user 条目。这将注册我们 crud 的所有相关资源路由。现在我们只需要测试一下。将浏览器指向 gastropod/users 路由,您应该会看到一个正确的 crud。

最后一件您可能想要做的事情是更新 Gastropod 模板视图,以将用户链接添加到导航菜单中。所以打开文件 resources\views\gastropod\template.blade.php 并按照以下方式修改其导航栏

<li class="nav-item">
  <a class="nav-link" aria-current="page" href="{{url('gastropod/users')}}">Users</a>
</li>

就这样了。祝您 Gastropoding 快乐!