简体   繁体   English

psr-0/4自动加载的后备位置

[英]Fallback location for psr-0/4 autoloading

I'm building a PHP package and in it I have a folder (let's say /vendor/myvendor/packagename/src/Classes ) which I want to autoload using PSR-4. 我正在构建一个PHP包,在其中我有一个文件夹(比方说/vendor/myvendor/packagename/src/Classes )我想用PSR-4自动加载。

But I would also like to provide an option to copy that folder from its current location to the project root (let's say /packagename/Classes , something along the lines of Laravel's publish command). 但我还想提供一个选项,将该文件夹从当前位置复制到项目根目录(比如/packagename/Classes ,这与Laravel的publish命令一致)。

So how could I go about autoloading it? 那么我该如何进行自动加载呢?

I would like Composer to first see if the folder exists under the app's root, and if it does then autoload that. 我希望Composer首先看看该文件夹是否存在于应用程序的根目录下,如果确实存在,则自动加载。 Else fall back to the default location inside /vendor . 否则回退到/vendor的默认位置。 Is that possible? 那可能吗?

FWIW, this is Laravel specific package, which means that I could use Laravel's publish command to copy the entire folder where ever, but then FWIW,这是Laravel特定包,这意味着我可以使用Laravel的publish命令来复制整个文件夹,但随后

  1. I would have to manually add the new location to autoload; 我必须手动将新位置添加到自动加载;
  2. Even if I do, there would be namespace conflict between the old and new location. 即使我这样做,旧位置和新位置之间也会存在名称空间冲突。

I would like Composer to first see if the folder exists under the app's root, and if it does then autoload that. 我希望Composer首先看看该文件夹是否存在于应用程序的根目录下,如果确实存在,则自动加载。 Else fall back to the default location inside /vendor. 否则回退到/ vendor中的默认位置。 Is that possible? 那可能吗?

Yes, it is possible. 对的,这是可能的。

You need to mention everything on your ServiceProvider for your package. 您需要在ServiceProvider提及包的所有内容。

There are every informations about how to publish your views, assets in the docs and it fits your needs. 有关如何在文档中发布视图,资产以及它符合您需求的各种信息。

Have a look at the docs: 看看文档:

https://laravel.com/docs/5.3/packages#public-assets https://laravel.com/docs/5.3/packages#public-assets

I am copying some example for you over here: 我在这里为你复制一些例子:

If you want to publish your translations, you need to perform this task: 如果要发布翻译,则需要执行以下任务:

/**
 * Perform post-registration booting of services.
 *
 * @return void
 */
public function boot()
{
    $this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'courier');

    $this->publishes([
        __DIR__.'/path/to/translations' => resource_path('lang/vendor/courier'),
    ]);
}

If you don't have translation in app's lang/vendor/courier then it falls back to your package translation. 如果您没有app's lang/vendor/courier翻译,那么它将回退到您的包翻译。

You don't need to copy your class codes, which could remain in your package directory. 您无需复制类代码,这些代码可能保留在您的包目录中。 The only things you copy will be your views, assets, translation files etc. 您复制的唯一内容将是您的观点,资产,翻译文件等。

This should help you. 这应该对你有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM