janole/laravel-composer-package

Just a demo composer package for Laravel ...

v1.0.0 2019-05-01 17:10 UTC

This package is auto-updated.

Last update: 2024-04-23 20:04:20 UTC


README

This is a demo composer package for Laravel. It will show you how to add views, routes and even an artisan command. You could use this project as a starting point for creating your own packages 😉.

Installation

Just require the package in your Laravel app:

composer require janole/laravel-composer-package

Now you will have a new artisan command:

$ php artisan demo:command
Hello, world!

And there will be some demo routes as well:

$ php artisan route:list
+--------+----------+-----------+------+---------+--------------+
| Domain | Method   | URI       | Name | Action  | Middleware   |
+--------+----------+-----------+------+---------+--------------+
|        | GET|HEAD | /         |      | Closure | web          |
|        | GET|HEAD | api/v1/me |      | Closure | api          |
|        | GET|HEAD | hello     |      | Closure |              |
|        | GET|HEAD | me        |      | Closure | web          |
+--------+----------+-----------+------+---------+--------------+

How does this work? (... is it magic? ... no!)

First, you need to register your package with Laravel.

Just extend an Illuminate\Support\ServiceProvider class and add it to an extra -> laravel -> providers block in the composer.json package description file:

{
    "name": "janole/laravel-composer-package",
    "type": "project",
    "description": "Just a demo composer package for Laravel ...",
    ...
    "extra": {
        "laravel": {
            "providers": [
                "janole\\Demo\\PackageServiceProvider"
            ]
        }
    }
}

Inside of the PackageServiceProvider

Use the boot() function of the extended provider class to register commands, routes or views:

class PackageServiceProvider extends ServiceProvider
{
    ...

    public function boot()
    {
        // Register an artisan command ... (php artisan demo:command)
        $this->commands([\janole\Demo\Console\Commands\DemoCommand::class]);

        // Register some demo routes
        $this->loadRoutesFrom(__DIR__.'/../routes/routes.php');

        // Register some demo views
        // (use them with the corresponding prefix like: @include("demo-views::the-view-name"))
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'demo-views');
    }
}

Using the views

When registering views, you can specify a prefix:

$this->loadViewsFrom(__DIR__.'/../resources/views', 'prefix');

Now, you can simply start using the views (Blade Templates) in your Laravel app like this:

@include("prefix::the-view-name", ...)

References