I normally store my locally developed packages inside thepackages
folder in my laravel projects root.
To ensure your core laravel application recognizes these customer packages we have to update our maincomposer.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'sthephpx
folder.
Then i create the package folder inside the namespace folder. For example if I am creating a demo package, I will create ademo
folder inside thethephpx
folder and then create acomposer.json
file inside the demo folder.
Thecomposer.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 thecomposer.json
file is created, then I create thesrc
folder and inside thesrc
folder createHttp\Controllers
folder. In a custom package thesrc
folder can have same folders as there are in the core laravel applicationsapp
folder.
Also inside thesrc
folder I create theDemoServiceProvider.php
class. Which is the custom service provider class for this custom package.
The content of theDemoServiceProvider.php
class is as follows:
<?phpnamespace 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 theroutes
folder I then createweb.php
route file. The content of the file is as follows:
<?phpuse 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 usedweb
middleware which never had to be mentioned explicitly in the core laravelweb.php
route file. But, in order for your package to get session data you need to include it in your package route file. Otherwiseauth()
helper or other Auth related methods does not work as expected.
Once everything is in place runcomposer install
inside the package folder where you have created it's owncomposer.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 visithttps://www.laravelpackage.com/
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse