PHP Laravel StrongAdmin 開源后台管理框架


Larevel-StrongAdmin

在1分鍾內構建一個功能齊全的管理后台。

基於 layui 前端框架開發的 Laravel 后台管理框架。同時擁有 api 接口,配合前端 VUE 開發。功能如下:

  • 權限管理
  • 菜單管理
  • 角色管理
  • 日志記錄
  • 管理員賬號

演示站點

http://demo.strongadmin.strongshop.cn/strongadmin

演示賬號:admin
演示密碼:123456

查看接口文檔

點擊查看

安裝

你可以使用 Composer 在 Laravel 5|6|7|8 項目中安裝 laravel-strongadmin 擴展:

composer require openstrong/laravel-strongadmin

安裝 laravel-strongadmin 后,可以在 Artisan 使用 strongadmin:install 命令來配置擴展實例。安裝 laravel-strongadmin 后,還應運行 migrate 命令:

php artisan strongadmin:install

php artisan migrate

瀏覽

http://你的域名/strongadmin

更新 laravel-strongadmin

更新 laravel-strongadmin 時,您應該重新配置加載 laravel-strongadmin 實例:

php artisan strongadmin:publish

配置

使用 laravel-strongadmin,其主要配置文件將位於 config/strongadmin.php。每個配置選項都包含其用途說明,因此請務必徹底瀏覽此文件。

/*
  |--------------------------------------------------------------------------
  | 啟用 StrongAdmin
  |--------------------------------------------------------------------------
 */
'enabled' => env('STRONGADMIN_ENABLED', true),

/*
  |--------------------------------------------------------------------------
  | StrongAdmin 子域名
  |--------------------------------------------------------------------------
  |
  | 設置后即可支持域名訪問
  |
 */
'domain' => env('STRONGADMIN_DOMAIN', null),

/*
  |--------------------------------------------------------------------------
  | StrongAdmin Path
  |--------------------------------------------------------------------------
  |
  | StrongAdmin 訪問路徑(也是路由前綴),如果修改此項,請記得修改以下配置 `ignore_auth_check_url`、`ignore_permission_check_url`
  |
 */
'path' => env('STRONGADMIN_PATH', 'strongadmin'),

/*
  |--------------------------------------------------------------------------
  | StrongAdmin 數據配置
  |--------------------------------------------------------------------------
  |
  | 1.在這可以自定義 StrongAdmin 數據庫連接的數據庫
  | 2.修改默認 后台超級管理員 賬號信息(僅安裝初始化有效)
  | 3.修改圖片驗證碼配置
  |
 */
'storage' => [
    //數據庫
    'database' => [
        'connection' => env('DB_CONNECTION', 'mysql'), //數據庫連接
    ],
    //后台超級管理員(僅安裝初始化有效)
    'super_admin' => [
        'user_name' => 'admin', //賬號名稱
        'password' => '123456', //賬號密碼
    ],
    //登錄限制
    'throttles_logins' => [
        'maxAttempts' => 5, //允許嘗試登錄最大次數
        'decayMinutes' => 10, //登錄錯誤超過 maxAttempts 次, 禁止登錄 decayMinutes 分鍾
    ],
    //圖片驗證碼
    'captcha'=>[
        'length' => 4, //字符長度
        'width' => 120, //寬
        'height' => 44, //高
        'expire' => 60, //有效期 秒
    ],
],

/*
  |--------------------------------------------------------------------------
  | StrongAdmin 中間件
  |--------------------------------------------------------------------------
  |
 */
'middleware' => [
    'web',
    OpenStrong\StrongAdmin\Http\Middleware\Auth::class, //登錄認證檢測
    OpenStrong\StrongAdmin\Http\Middleware\CheckPermission::class, //權限檢測
    OpenStrong\StrongAdmin\Http\Middleware\Log::class, //日志記錄
],

/*
  |--------------------------------------------------------------------------
  | StrongAdmin Auth Guard 登錄認證看守器名稱。不建議修改此項,如果修改此項則必須修改相對應的 `config/auth.php` 里的 `guards` 配置項
  |--------------------------------------------------------------------------
  | auth('strongadmin')->user() --- 獲取登錄用戶信息
  | auth('strongadmin')->id()   --- 獲取登錄用戶id
 */
'guard' => 'strongadmin',

/*
  |--------------------------------------------------------------------------
  | 忽略登錄檢測的路由
  |--------------------------------------------------------------------------
 */
'ignore_auth_check_url' => ['strongadmin/login', 'strongadmin/logout', 'strongadmin/captcha'],

/*
  |--------------------------------------------------------------------------
  | 忽略權限檢測的路由
  |--------------------------------------------------------------------------
 */
'ignore_permission_check_url' => ['strongadmin'],

快速構建

使用 artisan 命令快速生成 CURD 增刪改查的控制器和視圖

此命令使用擴展包 laravel-strongstub,詳細文檔:https://gitee.com/openstrong/laravel-strongstub

  1. 執行strongstub:curd命令:
    php artisan strongstub:curd Strongadmin/TesetAdminUserController -m App\\Models\\StrongadminUser --view
    
    結果:
     A App\Models\StrongadminUser model does not exist. Do you want to generate it? (yes/no) [yes]:
     >
    
    Model created successfully.
    Controller created successfully.
    
    Route::any('strongadmin/tesetAdminUser/index', 'Strongadmin\TesetAdminUserController@index');
    Route::any('strongadmin/tesetAdminUser/show', 'Strongadmin\TesetAdminUserController@show');
    Route::any('strongadmin/tesetAdminUser/create', 'Strongadmin\TesetAdminUserController@create');
    Route::any('strongadmin/tesetAdminUser/update', 'Strongadmin\TesetAdminUserController@update');
    Route::any('strongadmin/tesetAdminUser/destroy', 'Strongadmin\TesetAdminUserController@destroy');
    
    id:
    user_name:
    password:
    remember_token:
    name:
    email:
    phone:
    avatar:
    introduction:
    status:
    last_ip:
    last_at:
    created_at:
    updated_at:
    
    {"id":"","user_name":"","password":"","remember_token":"","name":"","email":"","phone":"","avatar":"","introduction":"","status":"","last_ip":"","last_at
    ":"","created_at":"","updated_at":""}
    
    Blade View`F:\phpstudy_pro\WWW_openstrong\strongadmin\resources\views/strongadmin/tesetAdminUser/form.blade.php` created successfully.
    Blade View`F:\phpstudy_pro\WWW_openstrong\strongadmin\resources\views/strongadmin/tesetAdminUser/index.blade.php` created successfully.
    Blade View`F:\phpstudy_pro\WWW_openstrong\strongadmin\resources\views/strongadmin/tesetAdminUser/show.blade.php` created successfully.
    
  2. 添加路由:app/routes/web.php
Route::middleware(['strongadmin'])->group(function () {
    Route::any('strongadmin/tesetAdminUser/index', 'Strongadmin\TesetAdminUserController@index');
    Route::any('strongadmin/tesetAdminUser/show', 'Strongadmin\TesetAdminUserController@show');
    Route::any('strongadmin/tesetAdminUser/create', 'Strongadmin\TesetAdminUserController@create');
    Route::any('strongadmin/tesetAdminUser/update', 'Strongadmin\TesetAdminUserController@update');
    Route::any('strongadmin/tesetAdminUser/destroy', 'Strongadmin\TesetAdminUserController@destroy');
});
  1. 把路由添加到 權限菜單=》菜單管理

開發

新增控制器

app/Http/Controllers/Strongadmin/AdminUserController

這里一定要繼承控制器 '\OpenStrong\StrongAdmin\Http\Controllers\BaseController'

use \OpenStrong\StrongAdmin\Models\StrongadminUser;
class AdminUserController extends \OpenStrong\StrongAdmin\Http\Controllers\BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (!$request->expectsJson())
        {
            return $this->view('adminUser.index');
        }
        $model = StrongadminUser::query();
        $rows = $model->paginate();
        return ['code' => 200, 'message' => __('admin.Success'), 'data' => $rows];
    }
}

新增路由

Route::middleware(['strongadmin'])->group(function() {
    Route::any('strongadmin/product/index', 'Strongadmin\AdminUserController@index');
});

新增視圖

resources/views/strongadmin/adminUser/index.blade.php

這里一定要繼承視圖模板 strongadmin::layouts.app

@extends('strongadmin::layouts.app')

@push('styles')
    <style></style>
@endpush

@push('scripts')
    <script>
    //......
    </script>
@endpush

@section('content')
    <div class="st-h15"></div>
    <form class="layui-form st-form-search" lay-filter="ST-FORM-SEARCH">
        ...
    </form>
@endsection

@push('scripts_bottom')        
    <script>
    !function () {
        //...
    }();
    </script>
@endpush

重構

這里以重構登錄為例

  1. 重構控制器
    新建 app/Http/Controllers/Strongadmin/AdminAuthController

    class AdminAuthController extends \OpenStrong\StrongAdmin\Http\Controllers\AdminAuthController
    {
        public function login(Request $request)
        {
    
        }
    }
    
  2. 重構路由

    Route::middleware(['strongadmin'])->group(function() {
        Route::any('strongadmin/login', 'Strongadmin\AdminAuthController@login');
    });
    

使用此擴展包的開源項目

StrongShop 開源跨境商城 https://gitee.com/openstrong/strongshop

QQ群:557655631


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM