If you have questions, try to find the answers in the official documentation
I had some troubles getting started with package development in laravel 5.4, that's why this repository was created.
This repo is for helping you to have a quick and easy start with package development in laravel 5.4. It was inspired by jaiwalker's jaiwalker/setup-laravel5-package
Take a look at the build in package: Naoray/Test!
git clone https://github.com/Naoray/package-dev.git
and install the dependencies through composer:
composer install
this will also generate a key
for your application and copy the .env.example
file into an .env
file
In root directory create a folder called packages/your_github_name/your_package_name/src/
e.g. root/packages/Naoray/Test/src
Now navigate to the src
folder and create a file for your service provider, e.g. TestServiceprovider.php
.
To create a ServiceProvider simply run php artisan make:provider Your_Package_nameProvider
and cut the file from
app/providers
to your packages src/
directory.
Take a look at this file as an example.
In your src
folder create a new routes
folder in which you create your webroutes file named web.php
.
e.g. like here
In your Http
folder create a new directory called Controllers
. In this folder you can create your controller.
e.g. like here
In your src
folder create a new directory and call it config
. In it create a new file (e.g. test.php
) like this
In your src
folder create a new directory and call it resources/
and in this folder create a new folder named views/
. In the views/
folder you can create your own custom blade views.
(example file
Run php artisan make:command CommandName
, go to app/Console/
and cut out the Commands/
folder. Now go to your packages
src
folder and paste it.
Add the command to the ServiceProvider's command array and register the command like in this file)
public function register()
{
/**
* some other code ...
*/
if ($this->app->runningInConsole()) {
$this->commands([
commands\TestCommand::class
]);
}
}
In your src
folder create a facades/
folder. Within the facades/
folder create your facade files.
(example file
In your src
folder create a file named like the facade you created before.
(example file)
Note: you have to alias your facade with the 'facade file' in your ServiceProvider like:
$this->app->alias(Test::class, 'naoray-test');
The value you use to bind your facade has to be the same value like in the facade
s getFacadeAccessor()
method.
Change to your packages directory and run composer init
.
Add the following to your packages composer.json file:
"autoload": {
"psr-4": {
"your_github_name\\your_package_name\\": "src/"
}
},
In your root composer.json file add "Naoray\\Test\\": "packages/naoray/test/src/"
to psr-4
, like:
"psr-4": {
"App\\": "app/",
"your_github_name\\your_package_name\\": "packages/your_github_name/your_package_name/src"
}
Add your_github_name\your_package_name\your_packages_service_provider::class,
to the config/app.php
providers
and to the aliases array (optional)array like this:
// providers array
/*
* Package Service Providers...
*/
Naoray\Test\TestServiceProvider::class,
//...
// aliases array
'Test' => Naoray\Test\Facades\Test::class,
Run composer dump-autoload
- make sure there are no errors.
Now you are ready to test your package!
This package boilerplate demonstrates some basic functionality usage which you can reuse for your own package development. Such as using...
- routes
- config files
- controllers
- views
- facades
- testing
If you want me to add something just open an issue and describe your desire!