Laravel 5软件包开发 [英] Laravel 5 package development

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

问题描述

由于workbench已被删除,我无法在Laravel 5中创建软件包.

与该线程一样(如何在Laravel 5中创建软件包?) ,Goldorak建议我们必须自己创建自己的包结构.

那么,我该如何手动创建工作台并使一切准备好进行软件包开发?

解决方案

使用laravel Workbench软件包:

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

"illuminate/workbench": "dev-master"

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

'Illuminate\Workbench\WorkbenchServiceProvider'

现在您需要创建config/workbench.php文件,因为该文件已从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' => '',
];

在此配置文件中填写您的信息,然后您将可以使用workbench命令:

php artisan workbench vendor/name


创建自己的包结构

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

这是包装结构:

packages/
  vendor/
    awesome/
      src/
        Awesome.php
      composer.json

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

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

composer init

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

<?php namespace Vendor/Awesome;

class Awesome
{
    public static function printAwesomeness()
    {
        echo 'Awesome';
    }
}

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

"autoload": {
    "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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆