使用Composer自动加载器进行自定义代码管理? [英] Custom code management with the Composer auto loader?

查看:166
本文介绍了使用Composer自动加载器进行自定义代码管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一个新项目,在这里我使用 Composer 处理一些依赖项及其自动加载。



我只保留composer.json文件在VCS,而不是整个供应商目录,所以我不想开始添加我的代码。

$

解决方案

b
$ b

如何处理我自己的项目特定代码,

这其实很简单。从您的存储库中排除供应商目录是正确的方法。您的代码应存储在单独的位置(如src)。



使用 autoload 属性,以使该作曲家识别您的命名空间:

  {
autoload:{
psr-0:{
Acme:src /
}
}
}

假设你有psr-0标准的类名,它应该工作。下面是类名称及其在文件系统上的位置的一些示例:




  • Acme\Command\HelloCommand - > src / Acme / Command /HelloCommand.php

  • Acme\Form\Type\EmployeeType - > src / Acme / Form / Type / EmployeeType.php



记住要为每个类定义一个命名空间。以下是Acme \Command\HelloCommand的示例:

 <?php 

命名空间Acme \命令;

class HelloCommand
{
}



不要忘记在您的PHP控制器中包含自动加载器:

 <?php 

require' vendor / autoload.php';

阅读更多 PHP框架互操作性组上的PSR-0标准。



请注意,如果您编辑 composer.json ,您需要运行install,update或 dump-autoload 刷新自动加载器类路径。


I've started a new project, where I use Composer to handle some dependencies, as well as their auto-loading.

I only keep the composer.json file in the VCS, instead of the entire vendor directory, so I don't want to start adding my code in there.

How should I handle my own project specific code, so that it auto loads as well?

解决方案

This is actually very simple. Excluding vendors directory from your repository is the right approach. Your code should be stored in a separate place (like src).

Use the autoload property to make that composer recognizes your namespace(s):

{
    "autoload": {
        "psr-0": {
            "Acme": "src/"
        }
    }
}

Assuming you have class names following psr-0 standard, it should work. Below some example of class names and their locations on the file system:

  • Acme\Command\HelloCommand -> src/Acme/Command/HelloCommand.php
  • Acme\Form\Type\EmployeeType -> src/Acme/Form/Type/EmployeeType.php

Remember to define a namespace for each class. Here's an example of Acme\Command\HelloCommand:

<?php

namespace Acme\Command;

class HelloCommand
{
}

Don't forget to include the autoloader in your PHP controllers:

<?php

require 'vendor/autoload.php';

Read more on PSR-0 standard on PHP Framework Interoperability Group.

Note that if you edit composer.json, you need to either run install, update or dump-autoload to refresh the autoloader class paths.

这篇关于使用Composer自动加载器进行自定义代码管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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