Giter VIP home page Giter VIP logo

laravel-study's People

Contributors

atlaschiew avatar

Watchers

 avatar  avatar

laravel-study's Issues

How laravel-admin speeds up CRUD process?

based on sample from https://laravel-admin.org/docs/en/quick-start.

<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserController extends AdminController
{
    protected $title ='Users';

    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'));
        $grid->column('email', __('Email'));
        $grid->column('password', __('Password'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('password', __('Password'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    protected function form()
    {
        $form = new Form(new User());

        $form->textarea('name', __('Name'));
        $form->textarea('email', __('Email'));
        $form->textarea('password', __('Password'));

        return $form;
    }
}

All methods on sample are protected, means they are not accessible by public, so we know the public methods are surely located in adminController. Let's open up adminController.

<?php

namespace Encore\Admin\Controllers;

use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;

class AdminController extends Controller
{
    use HasResourceActions;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Title';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        //        'index'  => 'Index',
        //        'show'   => 'Show',
        //        'edit'   => 'Edit',
        //        'create' => 'Create',
    ];

    /**
     * Get content title.
     *
     * @return string
     */
    protected function title()
    {
        return $this->title;
    }

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['show'] ?? trans('admin.show'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }
}

We can see there are four public methods, index, show, edit, and create respectively.

  1. index build listing with use of grid object.
  2. show build details of row.
  3. edit build edit form.
  4. create build add form.

now open up parent class (https://github.com/z-song/laravel-admin/blob/v1.8.19/src/Controllers/HasResourceActions.php) and you will see there are another three new public methods, update, store & destroy.

  1. update update existing record.
  2. store add new record.
  3. destroy delete a row.

Many developer tools

  1. search code in all files
cd madxframework
grep -rnw './' -e 'search keywords'

# search only on php files
grep -rnw './' -e 'search keywords' --include=*.php
  1. run composer
cd madxframework
/usr/local/bin/ea-php82 ~/composer.phar update
  1. download composer.phar
# check latest composer.phar version in https://getcomposer.org/download/2.5.5/composer.phar
# then copy and run command below
cd ~
wget https://getcomposer.org/download/2.5.5/composer.phar

How facade works in laravel-admin?

in this post, i will take Admin facade as example.

# vendor/encore/laravel-admin/composer.json
    "extra": {
        "laravel": {
            "providers": [
                ...
            ],
            "aliases": {
                "Admin": "Encore\\Admin\\Facades\\Admin"
            }
        }
    }

shortly, app implement ArrayAccess, thus trigger app->make to produce a new Admin instance

i will explain in future. https://learnku.com/articles/37493.

How does laravel-admin display html table?

  1. laravel uses grid package (Encore\Admin\Grid) for this purpose.
  2. public function index is default entry point of each controller or parent (adminController) one takes place if none implemented in derived controller. So let's look at parent's index method.
    public function index(Content $content)
    {
		
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))

             # derived class must implement protected function grid()
            ->body($this->grid());
    }
  1. ok, we head back to look at grid function of derived class.
 /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article());

        $grid->model()->ordered();

        $grid->id('ID')->sortable();

        $grid->title()->editable();
        $grid->content()->editable();
        $grid->picture()->image();

        $grid->order()->orderable();

        $grid->created_at();
        $grid->updated_at();

        return $grid;
    }
  1. in point number 2, grid is input of content's body and content class implements rendable interface, so we can know caller (i guess is router) will call render() to draw html. Upon content rendering, row (Encore\Admin\Layout\Row) and column (Encore\Admin\Layout\Column) will be built accordingly.

Where is `admin` middleware group?

in config/admin.php,

    'route' => [
        'prefix' => '',
        'namespace'     => 'App\\Admin\\Controllers',
        'middleware'    => ['web', 'admin', 'deny'],
    ],

    # i wonder where is admin?

in app/Http/Kernel.php, i only have found both web (group) and deny (individual),

 protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        ...
    ];

    protected $routeMiddleware = [
       ...
        'deny' => DenyRoutes::class,
    ];

The answer is in vendor/encore/laravel-admin/src/AdminServiceProvider.php, look for $middlewareGroups.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.