Laravel 5 package development: where to start?
Everything you need to know is in the docs, as it should be.
Apart from that: Think of a package to be primarily being a Composer package. You are not only on/limited to the development path of Laravel packages but actually Composer's since it's the one that is in control of autoloading those. If the package happens to include Service Providers, Facades, Blade views etc. then it has become a package with Laravel integration. This is in line with the reason the workbench was removed: to have a PHP wide solution.
A good starting point would be an existing project, ideally with a good set of use cases for the package.
At least during development of an application it gets clear what could or even should be separated into packages/libraries.
As an alternative create a new laravel project and build well defined use cases around the package.
The following is one possible way to develop packages (as SO mentioned, it's a subjective matter) that allows both "in-project" development and composer installs later on.
Disclaimer: I did not follow any tutorials nor did I specifically search for them since Composer and Laravel's docs provide everything needed. I just had a look at other Composer packages in the vendor
folder which leads me to believe it's a valid way. The following solution isn't even bound to Laravel - I use the same approach when developing Zend Framework moduls.
Note: Check that the package's namespace is not taken on packagist if you want to publish it there.
Set up a folder structure as may be found in other composer packages in a lib
or packages
folder in the project root.
lib/
my-namespace/
my-package/
config/
src/
Facades/
MyPackage.php
Support/
helpers.php
MyPackageServiceProvider.php
...
Add the package's src
folder (and additional files to be autoloaded) to the composer.json's autoload configuration of the laravel project. (See Composer's docs for available options)
"autoload": {
"files": [
"lib/my-namespace/my-package/src/Support/helpers.php"
],
"psr-4": {
"MyNamespace\\": "lib/my-namespace/my-package/src/"
}
},
Note: The my-namespace
folder is version controlled in its own repository. Therefor, the lib folder could be git-ignored on the project level.
Add Service Providers and Facades to Laravel's app configuration as mentioned in the docs.
Develop and use the package as described in the docs and seen in other Laravel packages.
Run composer dumpautoload
every time the application ignores recently made changes in your package/library.
If a package is meant to be made availabe publicly (e.g. github/packagist) it should include a minimum of the commonly expected software artefacts and ideally follow semantic versioning. Roughly described in folder contents:
docs/
tests/
composer.json
LICENSE
readme.md
Note: I tend to add a composer.json file in the package/library's root right at the beginning. It forces me to keep a clear idea of what this package provides and does not provide.
To publish the package/separate it from the project move related autoload parts from the project's to the library's composer.json
and adapt the paths. Then publish the project to packagist/own Toran proxy.
Require the package with --prefer-source
- this way it is possible to develop the package while in use, even in multiple different projects.
Laravel 5 package development
Using the laravel Workbench package:
You can add the illuminate/workbench
package in a Laravel 5 by adding to your composer.json
:
"illuminate/workbench": "dev-master"
then add the WorkbenchServiceProvider into your config/app.php
file:
'Illuminate\Workbench\WorkbenchServiceProvider'
Now you need to create the config/workbench.php
file since it has been removed from Laravel 5:
<?php
return [
/*
|--------------------------------------------------------------------------
| Workbench Author Name
|--------------------------------------------------------------------------
|
| When you create new packages via the Artisan "workbench" command your
| name is needed to generate the composer.json file for your package.
| You may specify it now so it is used for all of your workbenches.
|
*/
'name' => '',
/*
|--------------------------------------------------------------------------
| Workbench Author E-Mail Address
|--------------------------------------------------------------------------
|
| Like the option above, your e-mail address is used when generating new
| workbench packages. The e-mail is placed in your composer.json file
| automatically after the package is created by the workbench tool.
|
*/
'email' => '',
];
Fill your information in this config file then you will be able to use the workbench command:
php artisan workbench vendor/name
Creating your own package structure
In this exemple we will create our package called awesome in a packages directory.
Here is the package structure:
packages/
vendor/
awesome/
src/
Awesome.php
composer.json
- Vendor: your vendor name, typically this is your github username.
- Awesome: the name of your package
- src: Where you put the business logic
To generate a composer.json file you can use this command in the packages/vendor/awesome
directory:
composer init
Now we create a Awesome.php
class in the src
directory with a simple method:
<?php namespace Vendor/Awesome;
class Awesome
{
public static function printAwesomeness()
{
echo 'Awesome';
}
}
After that we add the package to the laravel composer.json
psr-4 autoloader:
"autoload": {
"psr-4": {
"App\\": "app/",
"Vendor\\Awesome\\": "packages/vendor/awesome/src"
}
},
and we dump the composer autoloader
composer dump-autoload
Now you can use your package everywhere in your laravel 5 project. If you need some laravel specific feature like service provider or view publishing, use them as described in the Laravel 5.0 documentation.
Laravel 5 package development clarity
Your question looks a little bit confuse. I develop packages for Laravel and the following is a regular way:
- Laravel manage its dependencias via composer, take a look into
composer.json
to get a clue how similiar is withbower
. - In order to get yout package compatible with laravel's core you need to implement some interfaces in your package. This package also can manage dependencies via composer.
- A package can be created as a repository in different version controls, like Github, BitBucket, Packagist, Cartalyst, private packages repositories, etc. By default laravel pull packages from Packagist, but into composer.json file you can specify another reposository as needed.
- When you trigger
composer update
(this is an equivalent asbower update
), this dependencies manager will pull all the packages and download them automatically invendor/
directory.
How to code your package while testing with laravel? some people do the following, including me:
- Install a laravel instance just for package development purpose.
- Create a new project (your package project) inside of
vendor/project-name
following lavavel's package requirements. - Keep working your package from this project location. By this way the changes are reflecting instantly in laravel installation.
- Don't forget to commit and push
How create package in Laravel 5?
The laravel workbench has been renamed in laravel 5 to "Package Development" in the documentation
http://laravel.com/docs/master/packages
Notice that there is no longer a workbench command and you need to create your own package structure, as the Laravel creator want to limit the dependency between created packages and the Laravel framework (#ref)
UPDATE: Laravel 5 is now stable and the illuminate/workbench
package can be used in a laravel 5 application as I suggested in this post
Laravel 5 Package Development GIT setup
If you're using composer/packagist then yes, you should add the folder packages to .gitignore on your main repository. When deploying the entire application or when updating a package you'll have to do a composer install/update
to keep everything up to date.
Laravel 5.1 Package Development - loading packages dependencies in development
Because they are not part of the main autoload process
I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.
You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).
So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.
I.e: If this is your composer.json file
{
"name": "foo/bar",
"require": {
"google/apiclient": "1.0.*"
}
}
This code will work
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.
How do I add artisan to my Package and Improve my Laravel Package Development Workflow?
I think 'Laravel Packager' is something you are looking for you can find more about this here
https://github.com/Jeroen-G/laravel-packager/blob/master/readme.md
laravel 5.* - package driven development - best practices
The great thing about Laravel, there's almost always a package for what you want to do. You can either create your packages as packages on composer, or you can install a module package and create them as modules.
For my projects I tend to use caffeinated/modules. I've tried a few and IMO it's the easiest to use.
Related Topics
Php: How to Get a List of Classes That Implement Certain Interface
How to Design a Hierarchical Role Based Access Control System
PHP "Pretty Print" JSON_Encode
Pass PHP Variable to Bootstrap Modal
Nohup:Ignoring Input and Appending Output to 'Nohup.Out'
Strange Behavior When Overriding Private Methods
Phpexcel Reader -- Help Required
How to Expire PHP Session If User Is Inactive for 15 Mins
How to Pass a Parameter Like Title, Summary and Image in a Facebook Sharer Url
How to Write to the Console from a Laravel Controller
Extract Url's from a String Using PHP
Get All the Images from a Folder in PHP
Using Zend Framework for Highload Projects
How to Show an Alert Box in PHP