/basicpackage

Basic of service provider and package development

Primary LanguagePHP

Basic Service Provider

Basic Package Development

  1. Create a folder /packages in root folder
  2. Every package has a name consists of 2 parts [vendor / creator] / [packagename] eg:. pk/basic, here "pk" is vendor and "basic" is package name
  3. Inside basic directory run composer init It will ask some questions to create composer.json file for your basic package, something like

{ "name": "pk/basic", "description": "Basic package for learning", "authors": [ { "name": "prafullakumarsahu", "email": "prafullakumarsahu001@gmail.com" } ], "require": {}, "minimum-stability": "stable", }

  1. In main composer.json (project root folder)

Make our package visible

"repositories": [ { "type": "path", "url": "./packages/pk/basic/", "options": { "symlink": true } } "require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "5.8.*", "laravel/passport": "^7.2", "laravel/tinker": "^1.0", "pk/basic": "dev-master" },
  1. run composer update

  2. Creating a service provider

Run artisan command php artisan make:provider BasicServiceProvider, it will generate a BasicServiceProvider.php file in app/providers, let's move this to packages/pk/basic/src directory and then change the namespace at top of the file

  1. From Laravel 5.5 there's a great function called auto-discovery, so in package's composer.json

Add

"autoload": { "psr-4": { "Pk\": "src/" } }, "extra": { "laravel": { "providers": [ "Pk\Basic\BasicServiceProvider" ] } }

  1. Create a controller, suing artisan command php artisan make:controller BasicControlller and move to packages/pk/basic/src directory and change the namespace at top of the file

  2. In side packages/pk/basic/src create a directory called routes/ and inside that create file web.php

Now add a route

Route::get('basic', 'Pk\Basic\BasicController@index')->name('basic');
  1. In BasicController's index() method
return 'Hello, I am your basic package.';
  1. In BasicServiceProvider register() method
public function register() { $this->app->make('Pk\Basic\BasicController'); }

and in boot() method

public function boot() { $this->loadRoutesFrom(__DIR__ . '/routes/web.php'); }
  1. Now run php artisan serve and visit 127.0.0.1:8000/basic

  2. Want to add your view and publish using php artisan vendor:publish? Create a view directory inside packages/pk/Basic/src/ directory and in BasicServiceProvider's boot() add

$this->publishes([ __DIR__.'/views' => base_path('resources/views/pk/basic'), ]);
  1. Run php artisan vendor:publish, publish the view and in BasicController's index() return view('pk.basic.index');