I normally store my locally developed packages inside the packages
folder in my laravel projects root.
To ensure your core laravel application recognizes these customer packages we have to update our main composer.json
file and add the following line :-
"repositories": [
{
"type": "path",
"url": "packages/thephpx/*"
}
]
Notice I have used *
in the package url, that is because i want composer to load all available packages inside the packages folder with the given namespace.
Custom Packages:
Before creating any new custom package I create my namespace folder first inside the packages folder. In my case it's thephpx
folder.
Then i create the package folder inside the namespace folder. For example if I am creating a demo package, I will create a demo
folder inside the thephpx
folder and then create a composer.json
file inside the demo folder.
The composer.json
file content looks as follows:
{
"name": "guestguide/demo",
"type": "library",
"description": "Demo package from thephpx",
"license": "MIT",
"autoload": {
"psr-4": {
"Guestguide\\Demo\\": "src/"
}
},
"authors": [
{
"name": "Faisal Ahmed",
"email": "thephpx@gmail.com"
}
],
"extra": {
"laravel": {
"providers": [ "Thephpx\\Demo\\DemoServiceProvider"
]
}
},
"require-dev": {
"orchestra/testbench": "^8.0"
}
}
Once the composer.json
file is created, then I create the src
folder and inside the src
folder create Http\Controllers
folder. In a custom package the src
folder can have same folders as there are in the core laravel applications app
folder.
Also inside the src
folder I create the DemoServiceProvider.php
class. Which is the custom service provider class for this custom package.
The content of the DemoServiceProvider.php
class is as follows:
<?php
namespace Thephpx\Dashboard;
use Illuminate\Support\ServiceProvider;
class DemoServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
//
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'dashboard');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../resources/assets' => public_path('dashboard'),
],'assets');
}
}
}
Then, I go on to create the other related folders such as \resources\views
, \resources\assets
, routes
etc.
Inside the routes
folder I then create web.php
route file. The content of the file is as follows:
<?php
use Illuminate\Support\Facades\Route;
Route::group(['middleware'=>'web'], function () {
Route::get('/demo', \Thephpx\Demo\Http\Controllers\DemoController::class)->middleware('auth');
});
Note: here in the web.php I have used web
middleware which never had to be mentioned explicitly in the core laravel web.php
route file. But, in order for your package to get session data you need to include it in your package route file. Otherwise auth()
helper or other Auth related methods does not work as expected.
Once everything is in place run composer install
inside the package folder where you have created it's own composer.json
file and install the required testbench library. Now you are ready to start developing your custom package.
This is suppose to be a basic startup document for further in-depth details you can visit https://www.laravelpackage.com/
Top comments (0)