Skip to content

Commit 22bc1d9

Browse files
author
songzou
committed
first commit
0 parents  commit 22bc1d9

File tree

9 files changed

+344
-0
lines changed

9 files changed

+344
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
phpunit.phar
3+
/vendor
4+
composer.phar
5+
composer.lock
6+
*.project
7+
.idea/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Jens Segers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
laravel-admin-config
2+
====================
3+
4+
[![StyleCI](https://styleci.io/repos/97664136/shield?branch=master)](https://styleci.io/repos/97664136)
5+
[![Packagist](https://img.shields.io/packagist/l/encore/laravel-admin-config.svg?maxAge=2592000)](https://packagist.org/packages/encore/laravel-admin-config)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/encore/laravel-admin-config.svg?style=flat-square)](https://packagist.org/packages/encore/laravel-admin-config)
7+
8+
[Demo](http://120.26.143.106/admin/config) use `username/password:admin/admin`
9+
10+
Inspired by https://github.com/laravel-backpack/settings.
11+
12+
## Installation
13+
14+
```
15+
$ composer require encore/laravel-admin-config
16+
17+
$ php artisan migrate
18+
19+
$ php artisan admin:import encore/laravel-admin-config
20+
```
21+
22+
Open `http://your-host/admin/config`
23+
24+
## Usage
25+
26+
After add config in the panel, use `config($key)` to get value you configured.
27+
28+
License
29+
------------
30+
Licensed under [The MIT License (MIT)](LICENSE).

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "laravel-admin-ext/config",
3+
"description": "Config extension for laravel-admin",
4+
"type": "library",
5+
"keywords": ["laravel-admin", "setting"],
6+
"homepage": "https://github.com/laravel-admin-extensions/config",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "z-song",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0.0",
16+
"laravel/framework": "5.5.*",
17+
"encore/laravel-admin": "1.5.*"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "~6.0",
21+
"laravel/laravel": "5.*"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Encore\\Admin\\Config\\": "src/"
26+
}
27+
},
28+
"extra": {
29+
"laravel": {
30+
"providers": [
31+
"Encore\\Admin\\Config\\ConfigServiceProvider"
32+
]
33+
34+
}
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateSettingsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
$connection = config('admin.database.connection') ?: config('database.default');
17+
18+
$table = config('admin.extensions.config.table', 'admin_config');
19+
20+
Schema::connection($connection)->create($table, function (Blueprint $table) {
21+
$table->increments('id');
22+
$table->string('name')->unique();
23+
$table->string('value');
24+
$table->text('description');
25+
$table->timestamps();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
$connection = config('admin.database.connection') ?: config('database.default');
37+
38+
$table = config('admin.extensions.config.table', 'admin_config');
39+
40+
Schema::connection($connection)->dropIfExists($table);
41+
}
42+
}

src/Config.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Encore\Admin\Config;
4+
5+
use Encore\Admin\Auth\Database\Menu;
6+
use Encore\Admin\Auth\Database\Permission;
7+
use Encore\Admin\Extension;
8+
use Illuminate\Support\Facades\Route;
9+
10+
class Config extends Extension
11+
{
12+
/**
13+
* Bootstrap this package.
14+
*
15+
* @return void
16+
*/
17+
public static function boot()
18+
{
19+
static::registerRoutes();
20+
21+
static::loadConfig();
22+
}
23+
24+
/**
25+
* Register routes for laravel-admin.
26+
*
27+
* @return void
28+
*/
29+
protected static function registerRoutes()
30+
{
31+
/* @var \Illuminate\Routing\Router $router */
32+
Route::group(['prefix' => config('admin.route.prefix')], function ($router) {
33+
34+
$attributes = array_merge([
35+
'middleware' => config('admin.route.middleware'),
36+
], static::config('route', []));
37+
38+
Route::group($attributes, function ($router) {
39+
40+
/* @var \Illuminate\Routing\Router $router */
41+
$router->resource('config', 'Encore\Admin\Config\ConfigController');
42+
});
43+
44+
});
45+
}
46+
47+
protected static function loadConfig()
48+
{
49+
foreach (ConfigModel::all(['name', 'value']) as $config) {
50+
config([$config['name'] => $config['value']]);
51+
}
52+
}
53+
54+
public static function import()
55+
{
56+
$lastOrder = Menu::max('order');
57+
58+
// Add a menu.
59+
Menu::create([
60+
'parent_id' => 0,
61+
'order' => $lastOrder + 1,
62+
'title' => 'Config',
63+
'icon' => 'fa-toggle-on',
64+
'uri' => 'config',
65+
]);
66+
67+
// Add a permission.
68+
Permission::create([
69+
'name' => 'Admin Config',
70+
'slug' => 'ext.config',
71+
'http_path' => admin_base_path('config*'),
72+
]);
73+
}
74+
}

src/ConfigController.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Encore\Admin\Config;
4+
5+
use Encore\Admin\Controllers\ModelForm;
6+
use Encore\Admin\Facades\Admin;
7+
use Encore\Admin\Form;
8+
use Encore\Admin\Grid;
9+
use Encore\Admin\Layout\Content;
10+
11+
class ConfigController
12+
{
13+
use ModelForm;
14+
15+
/**
16+
* Index interface.
17+
*
18+
* @return Content
19+
*/
20+
public function index()
21+
{
22+
return Admin::content(function (Content $content) {
23+
$content->header('Config');
24+
$content->description('Config list..');
25+
26+
$content->body($this->grid());
27+
});
28+
}
29+
30+
/**
31+
* Edit interface.
32+
*
33+
* @param $id
34+
*
35+
* @return Content
36+
*/
37+
public function edit($id)
38+
{
39+
return Admin::content(function (Content $content) use ($id) {
40+
$content->header('header');
41+
$content->description('description');
42+
43+
$content->body($this->form()->edit($id));
44+
});
45+
}
46+
47+
/**
48+
* Create interface.
49+
*
50+
* @return Content
51+
*/
52+
public function create()
53+
{
54+
return Admin::content(function (Content $content) {
55+
$content->header('header');
56+
$content->description('description');
57+
58+
$content->body($this->form());
59+
});
60+
}
61+
62+
public function grid()
63+
{
64+
return Admin::grid(ConfigModel::class, function (Grid $grid) {
65+
$grid->id('ID')->sortable();
66+
$grid->name()->display(function ($name) {
67+
return "<a tabindex=\"0\" class=\"btn btn-xs btn-twitter\" role=\"button\" data-toggle=\"popover\" data-html=true title=\"Usage\" data-content=\"<code>config('$name');</code>\">$name</a>";
68+
});
69+
$grid->value();
70+
$grid->description();
71+
72+
$grid->created_at();
73+
$grid->updated_at();
74+
75+
$grid->filter(function ($filter) {
76+
$filter->disableIdFilter();
77+
$filter->like('name');
78+
$filter->like('value');
79+
});
80+
});
81+
}
82+
83+
public function form()
84+
{
85+
return Admin::form(ConfigModel::class, function (Form $form) {
86+
$form->display('id', 'ID');
87+
$form->text('name')->rules('required');
88+
$form->textarea('value')->rules('required');
89+
$form->textarea('description');
90+
91+
$form->display('created_at');
92+
$form->display('updated_at');
93+
});
94+
}
95+
}

src/ConfigModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Encore\Admin\Config;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ConfigModel extends Model
8+
{
9+
/**
10+
* Settings constructor.
11+
*
12+
* @param array $attributes
13+
*/
14+
public function __construct($attributes = [])
15+
{
16+
parent::__construct($attributes);
17+
18+
$this->setConnection(config('admin.database.connection') ?: config('database.default'));
19+
20+
$this->setTable(config('admin.extensions.config.table', 'admin_config'));
21+
}
22+
}

src/ConfigServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Encore\Admin\Config;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ConfigServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function boot()
13+
{
14+
if ($this->app->runningInConsole()) {
15+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)