Now that you have a lot of articles on how to develop Laravel expansion pack. But most of the articles written too one-sided, incomplete, and when I actually developed expansion pack, or encountered many problems, I put my development experience, as well as problems encountered record, for everyone to share.
Extended Package Development
1. Create a new project, the expansion pack configuration initialization
First, create a new project Laravel:
composer create-project laravel/laravel laradmin -vvv
Next in this project, create a directory packages/{your_name}/{your_package_name}
mkdir packages / angkee / laradmin
Enter extended package directory, configuration initialization composer
cd packages/angkee/laradmin
composer init
After the execution, a next generation project composer.json
file:
{
"name": "angkee/laradmin", "description": "Laradmin is a laravel package for can be generated fast backend management panel", "license": "MIT", "authors": [ { "name": "angkee", "email": "[email protected]" } ], "require": {} }
2. Create the extension package base directory, file
Under normal circumstances, we will create the following files and directories:
angkee/laradmin
├── src #存放扩展包所有的逻辑代码
├── tests # 存放测试用例
├── README.md ├── composer.json └── LICENSE
3. modify extended configuration package composer
Then, modify this extension package composer.json
file, set about the composer
autoloading configuration, as well as expansion packs namespace.
{
..., "autoload": { "psr-4": { "Angkee\\Laradmin\\": "src/" } }, "autoload-dev": { "psr-4": { "Angkee\\Laradmin\\Tests\\": "tests/" } }, ... }
4. The write logic code extended package
Next, we create AdminServiceProvider.php
, Admin.php
file.
<?php
namespace Angkee\Admin; use Illuminate\Support\ServiceProvider; class AdminServiceProvider extends ServiceProvider { public function boot() { // } public function register() { $this->app->singleton('admin', function () { return new Admin; }); } }
<?php
namespace Angkee\Admin; class Admin { public function printRunning() { echo 'running'; } }
In this case, the expansion pack be developed well, then we start the local installation, testing.
Expansion Pack local test
To AdminServiceProvider
add to the project's config / app.php providers
array
'providers' => [
..., Angkee\Admin\AdminServiceProvider::class, ],
composer.json modify files in the project
{
"require": {
..., "angkee/laradmin": "dev-master" }, ..., "autoload": { ..., "psr-4": { ..., "Angkee\\Admin\\": "packages/laradmin/src/" } }, ... }
Run the command:
composer dump-autoload
composer update
最后,修改一下 routes/web.php
文件:
Route::get('/', function () { app('admin')->printRunning(); });
此时,我们打开浏览器访问此项目,显示 running
,恭喜你,成功了!
扩展包发布
扩展包开发、测试完成之后,这个时候就可以发布到 Packagist
。
1. 提交代码到 GitHub
首先,需要把扩展包的代码提交到 GitHub 上,记录下 GitHub 版本库的地址,注意是 HTTPS
。
2. 把扩展包发布到 Packagist
然后,访问 Packagist 官网,登录后,点击右上角 Submit 按钮,进入发布向导:
此时,将 GitHub 版本库的地址填写至 Repository URL
输入框中,然后点击 Submit 提交按钮,一切顺利,可以看到发布成功。
3. 设置代码同步
一旦在 Pakagist
上发布了包,之后的版本更新和代码同步,有一个机制来保证,就是 GitHub 中的事件通知服务,用于代码递交时触发一个事件,将代码同步到其他环境中。
添加服务,服务列表中选择 Packagist,主要填写两项 Packagist 配置信息:
用户名: 注意是 Packagist 上的用户名
Token: 通讯令牌
Domain: 可不用填写
其中 Token 需要到 Packagist 的个人设置里面去获取。
填写完毕,提交后,记得测试一次,完成首次同步,成功会提示信息。
Okay, the test payload is on its way.
Back Packagist, refresh, should not prompt again (Not Auto-Updated), indicating synchronization mechanisms already in force, GitHub after each change, will be automatically notified and synchronization.
4. Set the version information
Default version dev-master
, the version number Composer package will sync up from the tag in Git.
git tag 1.0.0
git push --tag
Just released expansion pack, this time installed, you may report an error can not find the installation package, you need to wait a bit server synchronization, but generally more than 3-5 minutes, if all goes well, you will see the version suggested that the installation was successful!
references
- Laravel the extension Development Guide
- All for one, one for all! Composer package to publish their communities
- Composer & Laravel local development package
Modest, self-discipline, confidence, do not reveal Ayutthaya