laravel-composer-package maintained by janole
laravel-composer-package
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 :wink:.
Installation
Just require the package in your Laravel app:
Now you will have a new artisan command:
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
-
Laravel Package Development from the official Laravel documentation
-
Laravel Package Development Basics from Pineco.de