Slide 1

Slide 1 text

CREATE AND PUBLISH YOUR OWN LARAVEL PACKAGE Mladen Janjetovic

Slide 2

Slide 2 text

WHO AM I? director of development at owner at using Laravel since 2014. working with PHP since 2006. team lead on international startups mentored frontend and backend developers SYSTEM-INC.COM TIRIEN.COM

Slide 3

Slide 3 text

PACKAGE?

Slide 4

Slide 4 text

the primary way of adding functionality to Laravel may be used with Laravel by simply requesting them in your le located in vendor/vendor_name composer.json

Slide 5

Slide 5 text

Can have it's own routes // Define within your package service provider's boot method public function boot() { $this->loadRoutesFrom(__DIR__.'/path/to/routes.php'); }

Slide 6

Slide 6 text

Can have it's own con g les // Publish configs within your package service provider's boot method public function boot() { $this->publishes([ __DIR__.'/path/to/config/my_package_config.php' => config_path('my_package_config.php'), ]); } Values may be accessed like any other con guration le config('my_package_config.some_option'); Con g can be merged with existing one $this->mergeConfigFrom( __DIR__.'/path/to/config/my_package_auth.php', 'auth' );

Slide 7

Slide 7 text

Recursive merging con g les - walkaround // service provider's boot method public function boot() { // Merge auth configurations $auth_config = array_merge_recursive( $this->app['config']['auth'], require __DIR__.'/config/auth.php' ); $this->app['config']->set('auth', $auth_config); }

Slide 8

Slide 8 text

If parent app routes are from cache -> get the real ones // service provider's boot method public function boot() { if (!$this->app->routesAreCached()) { require __DIR__.'/Http/routes.php'; } }

Slide 9

Slide 9 text

Can have it's own views // Define within your package service provider's boot method public function boot() { $this->loadViewsFrom(__DIR__.'/path/to/views', 'my_package'); } Package views are referenced like this Route::get('admin', function () { return view('my_package::dashboard'); });

Slide 10

Slide 10 text

Example of service provider's nal boot method public function boot() { // ensure we have most recent routes loaded if (!$this->app->routesAreCached()) { require __DIR__.'/Http/routes.php'; } // Merge auth configurations $auth_config = array_merge_recursive( $this->app['config']['auth'], require __DIR__.'/config/auth.php' ); $this->app['config']->set('auth', $auth_config); // load views $this->loadViewsFrom(__DIR__.'/resources/views/', 'admin'); }

Slide 11

Slide 11 text

Can have it's own migrations // Define within your package service provider's boot method public function boot() { $this->loadMigrationsFrom(__DIR__.'/path/to/migrations'); } When registered like this, they will automatically be run when the command is executed php artisan migrate Sometimes it's better to run package migration from package custom command i.e. instal CLI command

Slide 12

Slide 12 text

Less used package features // Define within your package service provider's boot method public function boot() { // translations $this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'my_package'); // CLI commands if ($this->app->runningInConsole()) { $this->commands([ InstallCommand::class, UpdateCommand::class, ]); } } Translations usage echo trans('my_package::messages.welcome'); Commands usage $ php artisan my_package:instal

Slide 13

Slide 13 text

With instal command we can do cool stu like this php artisan laravel-admin:instal which will install your package with style... :)

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Note that instal command must be called from environment which has access to DB server (Homestead, Vagrant)

Slide 16

Slide 16 text

Easy as this namespace SystemInc\LaravelAdmin\Console; use Artisan; use File; use Illuminate\Console\Command; use SystemInc\LaravelAdmin\Admin; class InstalCommand extends Command { protected $name = 'laravel-admin:instal'; protected $description = 'Instal Laravel Administration Essentials'; public function handle() { $name = $this->ask('Admin name', 'Admin'); $email = $this->ask('Admin email', '[email protected]'); $migrate = Artisan::call('migrate', [ '--path' => 'vendor/systeminc/laravel-admin/src/database/migrations', '--quiet' => true, ]); $this->line('Migrating Done!'); $this->line(''); $this->line('***'); $this->line(''); $this->line('Seeding...');

Slide 17

Slide 17 text

Laravel Admin Panel in two console commands

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

FORK - USE - CONTRIBUTE github.com/systeminc/laravel-admin it can be installed via composer, because it is published composer require systeminc/laravel-admin

Slide 23

Slide 23 text

PUBLISHED?

Slide 24

Slide 24 text

DEVELOPING PACKAGE develop the package in Laravel app under vendor/my_package directory write tests for your package use contracts instead of facades - easier to mock clone github repo in vendor/my_package directory we will need one for publishing and releasing

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

PUBLISHING PACKAGE create in your package root that will describe your package to composer publish to packagist, packalyst, or some other package directory that composer can use as repository by default Composer just uses the repository create release on github repo, you will need one for packagist submition you will need to update your packagist repo manualy, or to setup for Packagist composer.json packagist.org GitHub Service Hook

Slide 28

Slide 28 text

CUSTOM COMPOSER REPOSITORIES "repositories": [ { "type": "composer", "url": "http://packages.example.com" }, { "type": "vcs", "url": "https://github.com/Seldaek/monolog" } ]

Slide 29

Slide 29 text

COMPOSER.JSON { "name": "systeminc/laravel-admin", "description": "Administration panel for Laravel framework", "homepage": "https://bitbucket.org/system-inc/laravel-admin", "keywords": ["Admin", "laravel"], "type": "library", "license": "MIT", "minimum-stability": "stable", "authors": [ { "name": "Mladen Janjetovic", "email": "[email protected]" }, { "name": "Nemanja Maric", "email": "[email protected]" } ], "autoload": { "psr-4": { "SystemInc\\LaravelAdmin\\": "src/" } }, "require": { "intervention/image": "^2.3", "barryvdh/laravel-dompdf": "^0.7.0",

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Write installation guide

Slide 34

Slide 34 text

LARAVEL COMMUNITY PROJECTS github.com/laravel-serbia open source contribution shoot ideas

Slide 35

Slide 35 text

THANK YOU CONTACT ME [email protected] [email protected] twitter.com/youndivian github.com/MladenJanjetovic rs.linkedin.com/in/mladenjanjetovic