Laravel 5 包开发
问题描述
我在 Laravel 5 中创建包时遇到问题,因为 workbench
已被删除.
在这个线程中(如何在 Laravel 5 中创建包?), Goldorak 建议我们必须自己创建自己的包结构.
那么,如何手动创建工作台并为包开发做好一切准备?
使用 laravel Workbench 包:
您可以通过添加到您的 composer.json
来在 Laravel 5 中添加 illuminate/workbench
包:
然后将 WorkbenchServiceProvider 添加到您的 config/app.php
文件中:
现在您需要创建 config/workbench.php
文件,因为它已从 Laravel 5 中删除:
在此配置文件中填写您的信息,然后您将能够使用工作台命令:
<小时>
创建自己的包结构
在本示例中,我们将在包目录中创建名为 awesome 的包.
这里是包结构:
- 供应商:您的供应商名称,通常是您的 github 用户名.
- 很棒:您的包裹名称
- src:放置业务逻辑的位置
要生成 composer.json 文件,您可以在 packages/vendor/awesome
目录中使用此命令:
现在我们用一个简单的方法在src
目录下创建一个Awesome.php
类:
之后我们将包添加到 laravel composer.json
psr-4 自动加载器:
然后我们转储作曲家自动加载器
现在你可以在你的 laravel 5 项目的任何地方使用你的包.如果您需要某些 Laravel 特定功能,例如服务提供者或视图发布,请按照 Laravel 5.0 文档 中的说明使用它们.>
I am having trouble to create package in Laravel 5 as workbench
has been removed.
As in this thread (How create package in Laravel 5?), Goldorak suggest that we have to create our own package structure ourselves.
So, how can I create the workbench manually and get everything ready for package development?
Using the laravel Workbench package:
You can add the illuminate/workbench
package in a Laravel 5 by adding to your composer.json
:
then add the WorkbenchServiceProvider into your config/app.php
file:
Now you need to create the config/workbench.php
file since it has been removed from Laravel 5:
Fill your information in this config file then you will be able to use the workbench command:
Creating your own package structure
In this exemple we will create our package called awesome in a packages directory.
Here is the package structure:
- 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:
Now we create a Awesome.php
class in the src
directory with a simple method:
After that we add the package to the laravel composer.json
psr-4 autoloader:
and we dump the composer autoloader
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 包开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!