Introduction

Juzaweb
Docs

Plugin is a Laravel package which was created to manage your large Laravel app using modules. A plugin is like a Laravel package, it has some views, controllers or models. This package is supported and tested in Laravel 6.

Create plugin

Creating a plugin is simple and straightforward. Run the following command to create a plugin.

php artisan plugin:make author/plugin-name

E.x:

php artisan plugin:make theanh/ecommerce
plugins/
|-- author/plugin-name/
    |-- src/
        |-- database/
          |-- factories/
          |-- migrations/
          |-- seeders/
        |-- Http/
          |-- Controllers/
          |-- Middleware/
          |-- Requests/
        |-- Models/
        |-- Providers/
          |-- PluginNameServiceProvider.php
        |-- resources/
          |-- assets/
              |-- js/
                |-- app.js
              |-- sass/
                |-- app.scss
          |-- lang/
          |-- views/
        |-- routes/
          |-- api.php
          |-- admin.php
    |-- tests/
    |-- composer.json
    |-- package.json
    |-- webpack.mix.js

Custom namespaces

When you create a new module it also registers new custom namespace for Lang, View and Config. For example, if you create a new module named blog, it will also register new namespace/hint blog for that module. Then, you can use that namespace for calling Lang, View or Config. Following are some examples of its usage: Calling Lang:

Lang::get('domain::group.name');

@trans('domain::group.name');

Calling View:

view('domain::index')

view('domain::partials.sidebar')

Hook actions

About

Helpers

Read document and example

Routes

Resource

Resource model

php artisan plugin:make-migration create_examples_table author/plugin-name
php artisan plugin:make-model Slider author/plugin-name
<?php

namespace Plugins\Example\Models\Movie;

use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Mymo\Core\Traits\ResourceModel;

class Slider extends Model
{
    use ResourceModel;

    protected $fieldName = 'name';// Define column to show in breadcrumb
    protected $fillable = [
        'name',
        // Fillable
    ]

    // Define column for seach by keyword
    protected $searchAttributes = [
        'name',
    ];
    // Your code
}

Resource controller

php artisan plugin:make-controller SliderController author/plugin-name
<?php

namespace Plugins\Example\Http\Controllers;

use Mymo\Backend\Http\Controllers\BackendController;
use Plugins\Example\Models\Slider;

class SliderController extends BackendController
{
    use ResourceController;

    protected $viewPrefix = 'example::slider'; // View prefix for resource

    // Make validator for store and update
    protected function validator(array $attributes)
    {
        $validator = Validator::make($attributes, [
            'name' => 'required|string|max:250',
        ]);

        return $validator;
    }

    // Make data json for index datatable
    public function getDataTable(Request $request) {
        $search = $request->get('search');
        $status = $request->get('status');

        $sort = $request->get('sort', 'id');
        $order = $request->get('order', 'desc');
        $offset = $request->get('offset', 0);
        $limit = $request->get('limit', 20);

        $query = Slider::query();

        if ($search) {
            $query->where('name', 'like', '%'. $search .'%');
        }

        if ($status) {
            $query->where('status', '=', $status);
        }

        $count = $query->count();
        $query->orderBy($sort, $order);
        $query->offset($offset);
        $query->limit($limit);
        $rows = $query->get();

        foreach ($rows as $row) {
            $row->edit_url = route('admin.example.edit', [$row->id]);
        }

        return response()->json([
            'total' => $count,
            'rows' => $rows
        ]);
    }

    protected function getModel()
    {
        return Slider::class;
    }

    protected function getTitle()
    {
        return trans('example::app.sliders');
    }
}

Resource route

Route::mymoResource('sliders', 'SliderController');

Post type

Register post type

use Mymo\Core\Facades\HookAction;

// Example
HookAction::registerPostType('movies', [
    'label' => trans('movie::app.movies'),
    'model' => \Plugins\Movie\Models\Movie\Movie::class,
    'menu_icon' => 'fa fa-film',
    'menu_position' => 10,
    'supports' => ['tag'],
]);

Register Taxonomy for Post Type

HookAction::registerTaxonomy('genres', 'movies', [
    'label' => trans('movie::app.genres'),
    'menu_icon' => 'fa fa-edit',
    'menu_position' => 6,
    'supports' => [
        'thumbnail'
    ],
]);

Or

HookAction::registerTaxonomy('genres', ['movies', 'tv-series'], [
    'label' => trans('movie::app.genres'),
    'menu_icon' => 'fa fa-edit',
    'menu_position' => 6,
    'supports' => [
        'thumbnail'
    ],
]);

Breadcrumb

Add Breadcrumb admin panel, you can extends Mymo\Backend\Http\Controllers\BackendController for your controller in admin and add by code:

$this->addBreadcrumb([
    'title' => 'Title',
    'url' => 'Url',
]);

E.x:

<?php

// Your code

use Mymo\Backend\Http\Controllers\BackendController;

class YourController extends BackendController
{
    // Your code

    public function index()
    {
        // Your code
        $this->addBreadcrumb([
            'title' => 'Title',
            'url' => 'Url',
        ]);
        // Your code
    }

    // Your code
}

Menu

Add admin menu

Add to your file actions

HookAction::addAdminMenu(
    trans('movie::app.sliders'), // Label menu
    'sliders', // Menu key
    [
        'icon' => 'fa fa-paint-brush', // Icon
        'position' => 10, // Position item
        'parent' => 'appearance', // parent (default: null)
    ]);

Assets

Libs added

Support

Form ajax

<form action="" method="post" class="form-ajax">
// Your code
</form>
@component('mymo_core::components.form', [
    'method' => 'method: post, put, get, ...',
    'action' => 'action url'
])
@component('mymo_core::components.form_resource', [
    'method' => $model->id ? 'put' : 'post',
    'action' =>  $model->id ?
        route('admin.posts.update', [$model->id]) :
        route('admin.posts.store')
])

Select image from file manager

@component('mymo_core::components.form_image', [
    'label' => trans('mymo_core::app.thumbnail'), // Label
    'name' => 'thumbnail', // Name for input
    'value' => $model->thumbnail, // Value to show image
])@endcomponent

E.x:

<div class="form-group">
    <label class="col-form-label" for="logo">Logo <span class="float-right"><a href="javascript:void(0)" data-input="logo" data-preview="preview-logo" class="file-manager"><i class="fa fa-edit"></i> Change image</a></span></label>
    <div id="preview-logo">
        <img src="{{ asset('mymo/styles/images/thumb-default.png') }}" alt="" class="w-25">
    </div>
    <input id="logo" class="form-control" type="hidden" name="logo" value="">
</div>

Commands

Make model

php artisan plugin:make-model ModelName author/plugin-name

Make controller

php artisan plugin:make-controller ModelName author/plugin-name