Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

faisal ahmed
faisal ahmed

Posted on

Loading custom laravel packages locally

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/*"        }    ]
Enter fullscreen modeExit fullscreen mode

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"    }}
Enter fullscreen modeExit fullscreen mode

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');    }  }}
Enter fullscreen modeExit fullscreen mode

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');});
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a full stack web application developer from Dhaka, Bangladesh
  • Location
    Dhaka, Bangladesh
  • Work
    Full Stack Web Application Developer at Freelance
  • Joined

More fromfaisal ahmed

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp