- Create a folder /packages in root folder
- Every package has a name consists of 2 parts [vendor / creator] / [packagename] eg:. pk/basic, here "pk" is vendor and "basic" is package name
- 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",
}
- 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"
},
-
run composer update
-
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
- 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"
]
}
}
-
Create a controller, suing artisan command
php artisan make:controller BasicControlller
and move topackages/pk/basic/src
directory and change the namespace at top of the file -
In side
packages/pk/basic/src
create a directory calledroutes/
and inside that create fileweb.php
Now add a route
Route::get('basic', 'Pk\Basic\BasicController@index')->name('basic');
- In
BasicController
'sindex()
method
return 'Hello, I am your basic package.';
- 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');
}
-
Now run
php artisan serve
and visit127.0.0.1:8000/basic
-
Want to add your view and publish using
php artisan vendor:publish
? Create a view directory insidepackages/pk/Basic/src/
directory and inBasicServiceProvider
'sboot()
add
$this->publishes([
__DIR__.'/views' => base_path('resources/views/pk/basic'),
]);
- Run
php artisan vendor:publish
, publish the view and inBasicController
'sindex()
return view('pk.basic.index');