Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Create and publish your own Laravel package

Create and publish your own Laravel package

We'll see how we can create our own reusable Laravel package and use it with Composer magic in our future projects.

mladenjanjetovic

December 15, 2016
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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'); }
  4. 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' );
  5. 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); }
  6. 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'; } }
  7. 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'); });
  8. 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'); }
  9. 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
  10. 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
  11. With instal command we can do cool stu like this

    php artisan laravel-admin:instal which will install your package with style... :)
  12. Note that instal command must be called from environment which

    has access to DB server (Homestead, Vagrant)
  13. 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...');
  14. FORK - USE - CONTRIBUTE github.com/systeminc/laravel-admin it can be installed

    via composer, because it is published composer require systeminc/laravel-admin
  15. 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
  16. 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
  17. CUSTOM COMPOSER REPOSITORIES "repositories": [ { "type": "composer", "url": "http://packages.example.com"

    }, { "type": "vcs", "url": "https://github.com/Seldaek/monolog" } ]
  18. 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",