如何讓普通使用者也用上 Laravel-admin 。。

zxk發表於2018-10-18

1 繼承 Encore\Admin\Layout\Content

<?php
namespace App\Http\Wrapper;
use Admin;
use App\Admin\Extensions\Form\CityPicker;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class UserContent extends Content
{
    protected $view = 'wrapper.content';

    public function __construct(\Closure $callback = null)
    {
        Form::registerBuiltinFields();
        Form::extend('city_picker', CityPicker::class);
        $assets = Form::collectFieldAssets();
        Admin::css($assets['css']);
        Admin::js($assets['js']);
        parent::__construct($callback);
    }

    public function render()
    {
        $items = [
            'title' => $this->header . ' ' . $this->description,
            'header' => $this->header,
            'description' => $this->description,
            'breadcrumb' => $this->breadcrumb,
            'content' => $this->build(),
        ];

        return view($this->view, $items);
    }

}

2 定義 pjax

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\MessageBag;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;

class MyPjax
{
    protected $auth_name = 'web';

    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (!$request->pjax() || $response->isRedirection() || Auth::guard()->guest()) {
            return $response;
        }

        if (!$response->isSuccessful()) {
            return $this->handleErrorResponse($response);
        }

        try {
            $this->filterResponse($response, $request->header('X-PJAX-CONTAINER'))
                ->setUriHeader($response, $request);
        } catch (\Exception $exception) {
        }

        return $response;
    }
    /// 其它內容和 Encore\Admin\Middleware\Pjax 一致
}

3 複製模版
模板跟laravel-admin 的模板一樣,除了 sidebar.blade.php 的選單,這種將選單寫死

  • admin.blade.php 跟 vendor/encore/laravel-admin/resources/views/index.blade.php 基本一樣
        @include('admin::partials.header')
    @include('admin::partials.sidebar')
    <div class="content-wrapper" id="pjax-container">
        @yield('content')
        {!! Admin::script() !!}
    </div>
    @include('admin::partials.footer')

    將模板路徑替換成自己的

  • header.blade.php 將使用者名稱稱,頭像用用 auth()->user->nameStorage::url(auth()->user()->avatar 代替
  • sidebar.blade.php 將選單部分用固定的菜

            <ul class="sidebar-menu">
            <li class="header">選單</li>
            <li>                <a href="/my/">
                    <i class="fa fa-dashboard"></i>
                    <span>首頁</span>
                </a>
            </li>

    4 建立控制器
     我們可以使用 php artisna admin:make PostController --model=App\Post 然後將生成的檔案移到 App\Http\Controller 下,修改namespace
    再將頂部的 use Encore\Admin\Layout\Content; 改為 use App\Http\Wrapper\UserContent as Content; 第一改寫的content 類, 再在這個控制器中加入 middle pjax(第二步定義的pjax,已在app/Http/Kernel.php中配置

         public function __construct()
    {
        $this->middleware('pax');
    }

    未完等待檢驗

相關文章