沃梦达 / 编程问答 / php问题 / 正文

Laravel 5 包开发

Laravel 5 package development(Laravel 5 包开发)

本文介绍了Laravel 5 包开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 5 中创建包时遇到问题,因为 workbench 已被删除.

在这个线程中(如何在 Laravel 5 中创建包?), Goldorak 建议我们必须自己创建自己的包结构.

那么,如何手动创建工作台并为包开发做好一切准备?

解决方案

使用 laravel Workbench 包:

您可以通过添加到您的 composer.json 来在 Laravel 5 中添加 illuminate/workbench 包:

"illuminate/workbench": "dev-master"

然后将 WorkbenchServiceProvider 添加到您的 config/app.php 文件中:

'IlluminateWorkbenchWorkbenchServiceProvider'

现在您需要创建 config/workbench.php 文件,因为它已从 Laravel 5 中删除:

'',/*|--------------------------------------------------------------------------|Workbench 作者电子邮件地址|--------------------------------------------------------------------------||与上面的选项一样,生成新的电子邮件地址时将使用您的电子邮件地址|工作台包.电子邮件放在您的 composer.json 文件中|在工作台工具创建包后自动执行.|*/'电子邮件' =>'',];

在此配置文件中填写您的信息,然后您将能够使用工作台命令:

php artisan Workbench 供应商/名称

<小时>

创建自己的包结构

在本示例中,我们将在包目录中创建名为 awesome 的包.

这里是包结构:

包/小贩/惊人的/来源/真棒.php作曲家.json

  • 供应商:您的供应商名称,通常是您的 github 用户名.
  • 很棒:您的包裹名称
  • src:放置业务逻辑的位置

要生成 composer.json 文件,您可以在 packages/vendor/awesome 目录中使用此命令:

composer init

现在我们用一个简单的方法在src目录下创建一个Awesome.php类:

<?php namespace Vendor/Awesome;课堂很棒{公共静态函数 printAwesomeness(){echo '真棒';}}

之后我们将包添加到 laravel composer.json psr-4 自动加载器:

自动加载":{psr-4":{"app\": "app/","Vendor\Awesome\": "packages/vendor/awesome/src"}},

然后我们转储作曲家自动加载器

composer dump-autoload

现在你可以在你的 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:

"illuminate/workbench": "dev-master"

then add the WorkbenchServiceProvider into your config/app.php file:

'IlluminateWorkbenchWorkbenchServiceProvider'

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 包开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5 包开发

基础教程推荐