简体   繁体   English

Composer、Laravel 和本地包

[英]Composer, Laravel and local packages

My issue is I have a package which isn't a repository and I am trying to get it to play nice with Laravel and composer.我的问题是我有一个不是存储库的包,我试图让它与 Laravel 和作曲家一起玩。 It is still located under the vendor folder, the only issue is that if I simply set:它仍然位于供应商文件夹下,唯一的问题是如果我只是设置:

"psr-0": {
        "Test\\Test": "vendor/test/test/src/"
    }

This will load the service provider but none of the controllers etc will autoload.这将加载服务提供者,但不会自动加载任何控制器等。 What is the correct way to implement a package with larval that does not have it's own repository.使用没有自己的存储库的幼虫实现包的正确方法是什么。 Or does this go against the nature of packages and this should simply be structured under the applications controllers.或者这是否违背了包的性质,这应该简单地在应用程序控制器下构建。

The package was created by me using workbench but I found i did not really need this as a separate repository but it would still be good to keep it as a package.该包是我使用工作台创建的,但我发现我并不真正需要它作为单独的存储库,但将它作为一个包保留仍然很好。 Therefore the structure is exactly the same as a regular package:因此结构与常规包完全相同:

vendor
    testvendor
        testpackage
            public
            src
            tests
            .gitignore
            composer.json
            phpunit.xml

UPDATE:更新:

As a solution for the time being I am using:作为暂时的解决方案,我正在使用:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "vendor/package"
    ]
},

As an entry in the class map.作为类映射中的条目。 Looking forward I will probably refactor this into the app folder or create a repository for this package.展望未来,我可能会将其重构到 app 文件夹中或为此包创建一个存储库。

If you have some classes that you're calling "package", you're not supposed to add those files to your vendor folder.如果您将某些类称为“包”,则不应将这些文件添加到供应商文件夹中。 This folder is managed by composer and at any time you might loose it.该文件夹由 Composer 管理,您随时可能会丢失它。 Create a subfolder in your application and put those files there.在您的应用程序中创建一个子文件夹并将这些文件放在那里。

You have to be sure your PSR-0 autoloading will work for every single file in your folder structure.您必须确保您的 PSR-0 自动加载适用于文件夹结构中的每个文件。 So, if your root is vendor/test/test/src/ and your namespace is因此,如果您的根目录是vendor/test/test/src/并且您的命名空间是

Test\\Test

All your files must be in您的所有文件都必须在

vendor/test/test/src/Test/Test/ClassFileName.php

PSR-4 is easier to deal and understand, this PSR-4 更容易处理和理解,这个

"psr-4": {
    "Test\\Test\\": "vendor/test/test/src/"
}

Means that your files would have to be like:意味着您的文件必须是这样的:

vendor/test/test/src/ClassFileName.php

Doublecheck your namespaces.仔细检查您的命名空间。 It's easy to make mistakes when using namespaces with PSR-0 and remember that使用带有 PSR-0 的命名空间时很容易出错,请记住

composer dump-autoload

Must be ran every time you change things in composer.json or create new files.每次更改 composer.json 中的内容或创建新文件时都必须运行。 If it's a simple class autoloading, every time you create a file, if it's a PSR-X autoloading, everytime you create or update a namespace in your composer.json file.如果是简单的类自动加载,则每次创建文件时,如果是 PSR-X 自动加载,则每次在 composer.json 文件中创建或更新命名空间时。

If what you have is is really a package you should use Composer: when your package is structured as a composer package (check Laravel's composer.json as an example), the correct way of adding it to your application, if it's not list in Packagist, is via repositories .如果你真的是一个包,你应该使用 Composer:当你的包被构造为一个 Composer 包时(以 Laravel 的 composer.json 为例),将它添加到你的应用程序的正确方法,如果它没有在 Packagist 中列出,是通过repositories

You can have (non-packagist) packages in a public VCS repository:您可以在公共 VCS 存储库中拥有(非打包者)包:

{
    "require": {
        "monolog/monolog": "dev-bugfix"
    },

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorw/monolog"
        }
    ]
}

You can have (non-packagist) packages in a protected by password VCS repository (git, bitbucket...):您可以在受密码保护的 VCS 存储库(git、bitbucket...)中拥有(非打包者)包:

{
    "require": {
        "vendor/my-private-repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@bitbucket.org:vendor/my-private-repo.git"
        }
    ]
}

You can have your packages zipped in your hard drive and load them via the artifact repository type:您可以将您的软件包压缩在您的硬盘驱动器中并通过artifact存储库类型加载它们:

"repositories": [
    {
        "type": "artifact",
        "url": "path/to/directory/with/zips/"
    }
],

Though @Antonio Carlos Ribeiro's answer is really nice, I had problem with installing custom packages locally(which is also stated in the last part of his answer)尽管@Antonio Carlos Ribeiro 的回答非常好,但我在本地安装自定义软件包时遇到了问题(这也在他回答的最后一部分中有所说明)

Let's assume this is the directory structure of the package we are trying to install:让我们假设这是我们尝试安装的包的目录结构:

D:/test_pack
    src/
    composer.json

If you do not want to upload your custom package (that most likely you have developed, yourself) to online repositories you can use one of the following two methods:如果您不想将自定义包(很可能是您自己开发的)上传到在线存储库,您可以使用以下两种方法之一:

Method I方法一

(You have to specify version for your package, otherwise you'll get this error: The requested package could not be found in any version, there may be a typo in the package name.) (你必须为你的包指定版本,否则你会得到这个错误:在任何版本中都找不到请求的包,包名中可能有拼写错误。)

1) In composer.json, Add version to your package. 1) 在 composer.json 中,将版本添加到您的包中。 your package's json should look something like this:你的包的 json 应该是这样的:

{
"name": "gandalf/test_pack",//This is your package's name
"description": "some desc",
"version": "1.0.0",//This is the version that you have to specify
"authors": [
    {
        "name": "gandalf the grey",
        "email": "fake@yahoo.com"
    }
],
"minimum-stability": "dev",
"require": {
    "laravel/framework": "~5.4"
},
"autoload": {
    "psr-4": {
        "Gandalf\\BotPack\\": "src/"
    }
} }

2) zip your package(let's assume the zip file is in D:/test_pack/test_packa.zip) 2) 压缩你的包(假设 zip 文件在 D:/test_pack/test_packa.zip 中)

3) In laravel's composer.json add your package name (in our case gandalf/test_pack into require part of json) and add the repository array to the composer.json file and in that array specify the directory in which your package's zip file exists(in our case D:/test_pack) . 3)在laravel的composer.json中添加你的包名(在我们的例子中gandalf/test_pack到json的require部分)并将存储库数组添加到composer.json文件中,并在该数组中指定包的zip文件所在的目录(在我们的例子中 D:/test_pack) 。 like this像这样

{
    ...,
    "require": {//adding our package name to laravel's composer.json
        ...,
        "gandalf/test_pack": "*"//package's name
    },
    ...,

    "repositories": [
        {
            "type": "artifact",
            "url": "D:/test_pack"
        }
    ]
}

Method II (My Favorite method, You have to initialize your package directory as git local repository using git init and then git add . and git commit -m "your message" )方法二(我最喜欢的方法,你必须使用git init将你的包目录初始化为 git 本地存储库,然后git add .git commit -m "your message"

1) initialize the package directory as git directory and commit all your changes to the local repository 1) 将包目录初始化为 git 目录并将所有更改提交到本地存储库

(let's say D:/test_pack is the directory that contains your package( src/ directory and composer.json )) (假设D:/test_pack是包含您的包的目录( src/目录和composer.json ))

go to D:/test_pack directory and run these commands转到D:/test_pack目录并运行这些命令

git init
git add .
git commit -m "your message for this commit"

2) In your packages composer.json file add minimum-stability 2) 在你的包composer.json文件中添加 minimum-stability

    {
        "name": "gandalf/test_pack",
        "description": "some desc",
        "authors": [
            {
                "name": "gandalf the grey",
                "email": "fake@yahoo.com"
            }
        ],
        "minimum-stability": "dev",//setting minimum-stability
        "require": {
               //dependencies that your package needs
        },
        "autoload": {
            "psr-4": {
                "Gandalf\\BotPack\\": "src/"
            }
        }
    }

3)In laravel's composer.json file require the "dev-master" of your package 3)在laravel的composer.json文件中需要你的包的“dev-master”

{
    ...,
    "require": {
        ...,//some dependencies that laravel needs
        "gandalf/test_pack": "dev-master"//requiring dev-master from repository
    },

    "repositories": [
        {
            "type": "git",
            "url": "D:/test_pack"//path of the local repository directory which contains your package
        }
    ]
}

To any Laravel project load local packages.任何 Laravel 项目加载本地包。 which is stored in your machine.它存储在您的机器中。

In laravel's (Project) composer.json file add在 laravel 的(项目) composer.json文件中添加

  "autoload": {
    "psr-4": {
      "YourPackage\\Namespace\\": "./local_Package_path/src"
    }
  },

and fire command in Laravel Project directory并在 Laravel 项目目录中触发命令

composer dump-autoload

Optional可选的

If package is still not available in your Project.如果您的项目中仍然没有包。 then Register your package's Service Provider.然后注册您的包的服务提供商。

To register your service provider, you just need to add an entry to the array of service providers in the config/app.php file.要注册您的服务提供者,您只需要在config/app.php文件中的服务提供者数组中添加一个条目。


'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    ...

    YourPackage\Namespace\PackageServiceProvider::class,
], 

Hope now your package loaded successfully in your laravel project.希望现在你的包在你的 Laravel 项目中成功加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 作曲家:程序包不在服务器上,但在本地计算机上找到 - Composer: Packages not found on server, but on local machine 未安装 Composer 本地存储库(适用于 Laravel) - Composer local repository not installing (for Laravel) Composer无法读取我的本地Satispackages.json文件 - Composer is not reading my local Satis packages.json file Laravel-在实时服务器上安装与Composer一起安装的软件包 - Laravel - Installing packages that were installed with composer on live server 将第三方作曲家程序包自动加载到Laravel 5 Application中 - Autoloading 3rd party composer packages into Laravel 5 Application Laravel清除作曲家软件包的软件包自动发现缓存 - Laravel clear package auto-discovery cache of composer packages Composer 安装 Laravel:无法将您的要求解析为一组可安装的软件包 - Composer install Laravel: Your requirements could not be resolved to an installable set of packages Laravel - PHP - 通过 Composer 安装 HTML 包失败 - Laravel - PHP - Installing HTML packages via composer failed php laravel 我在作曲家安装或删除软件包或为作曲家编写任何命令时遇到问题 - php laravel i have a problem in composer when install or remove packages or write any command for composer Laravel 8 运行 composer require laravel/passport 时,您的要求无法解决为一组可安装的软件包 - Laravel 8 Your requirements could not be resolved to an installable set of packages when running composer require laravel/passport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM