Laravel-admin文档地址:http://laravel-admin.org/docs/zh/installation
按照文档安装
后台菜单是存数据库的,在后台自行添加后将相应功能路由填入路径即可
接下来我们来做一个简单的功能。
例如:我们需要在菜单增加一个新闻管理
我们在/app/Http/Model创建模型News.php
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class News extends Base{
public $table = 'news';
public function getJsonStrAttribute($value)
{
return json_decode($value,true);
}
//保存jsonstr字段的时候会自动转化为json格式
public function setJsonStrAttribute($value)
{
$this->attributes['jsonstr'] = json_encode($value);
}
}
后台默认路由地址在:/app/Admin/routes.php
<?php
use Illuminate\Routing\Router;
Admin::routes();
Route::group([
'prefix' => config('admin.route.prefix'),
'namespace' => config('admin.route.namespace'),
'middleware' => config('admin.route.middleware'),
], function (Router $router) {
$router->get('/', 'HomeController@index')->name('admin.home');
/**
* 等同
* $router->resource('news', 'NewsController');
* $router->resource('shop',ShopController::class);
* Route::resource('news', 'NewsController');
*/
$router->resources([
'news'=>'NewsController'
]);
});
laravel-admin省去了很多常规代码,基本使用只要参照ExampleController.php里面的代码即可。
下面是第一次学习的代码,里面的代码是参照一位后辈的。
<?php
namespace App\Admin\Controllers;
use App\Http\Model\Shop;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;
use Encore\Admin\Grid;
class ShopController extends BaseController
{
use ModelForm;
protected $header = '商品列表';
/**
* Index interface.
*
* @return Content
*/
public function index(Content $content)
{
$content->header($this->header);//设置头部标题,等同全局变量$title
$content->description('列表'); //设置头部描述
$content->body($this->grid()); //调用原有函数
return $content;
}
public function test(){
}
/**
* Edit interface.
*
* @param $id
* @return Content
*/
public function edit($id,Content $content)
{
$content->header($this->header);
$content->description('编辑');
$content->body($this->form()->edit($id));
return $content;
}
/**
* Create interface.
*
* @return Content
*/
public function create(Content $content)
{
$content->header($this->header);
$content->description('新建');
$content->body($this->form());
return $content;
}
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new Shop());
$grid->id('ID');
$grid->name('商品名'); //等同 $grid->column('name', __('商品名'));
$options = [1 => 'foo', 2 => 'bar', 3 => 'Option name'];
$grid->listtype('类别')->map(function ($pictures) use($options) {
return array_key_exists($pictures,$options) ? $options[$pictures] : '';
})->label();
// 设置text、color、和存储值
$states = [
'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary']
];
$grid->status('状态')->switch($states);
$grid->created_at('创建时间');
$grid->updated_at('修改时间');
return $grid;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
return Admin::form(Shop::class, function (Form $form) {
//新建和修改都在这里
$form->text('name','商品名'); // 等同 $form->display('name', __('商品名'));
$form->switch('status','状态')->options(['0' => '关闭', '1'=> '开启'])->default('0');
$form->listbox('listtype','类别')->options([1 => 'foo', 2 => 'bar', 3 => 'Option name']);
});
}
protected function show($id, Content $content)
{
$content->header($this->header);
$content->description('详情');
$content->body(Admin::show(Shop::findOrFail($id), function (Show $show) {
$show->panel()->tools(function ($tools) {
$tools->disableEdit(); //禁用编辑视图
$tools->disableDelete(); //禁用删除视图
});
$show->id('ID');
$show->name('商品名称'); //等同 $show->field('name', __('商品名称'));
$options = [1 => 'foo', 2 => 'bar', 3 => 'Option name'];
$show->listtype()->as(function ($items) use($options){
foreach($items as &$val){
$val = array_key_exists($val,$options) ? $options[$val] : '';
}
return $items;
})->label();
}));
return $content;
}
}