简体   繁体   English

作曲家供应商/路径

[英]Composer vendor/ path

As stated anywhere on the web, a webroot folder should only contain an entry point (index.php) and the assets folder. 如网络上任何地方所述,webroot文件夹应仅包含入口点(index.php)和资产文件夹。 Application source code must go in an upper directory, not accessible throw the web. 应用程序源代码必须放在上层目录中,无法访问。

Since I'll have several domains using the same app, I did something like this: 由于我将使用同一个应用程序来管理多个域,因此我做了如下操作:

drwxrwxr-x 7 teo teo 4096 Apr  9 16:43 app
-rw-rw-r-- 1 teo teo  334 Apr  9 14:51 composer.json
-rw-rw-r-- 1 teo teo 9213 Apr  9 14:51 composer.lock
-rw-rw-r-- 1 teo teo 1965 Apr  9 13:01 deploy.ant
lrwxrwxrwx 1 teo teo   12 Apr  7 19:28 webroot2 -> app/webroot/
lrwxrwxrwx 1 teo teo   12 Apr  7 19:28 webroot3 -> app/webroot/
lrwxrwxrwx 1 teo teo   12 Apr  7 19:28 public_html -> app/webroot/
drwxrwxr-x 8 teo teo 4096 Apr  9 14:51 vendor

Each webroot folder is just a relative link to the folder app/webroot : 每个webroot文件夹只是指向文件夹app/webroot的相对链接:

rwxrwxr-x 6 teo teo 4096 Apr  7 19:27 assets
-rw-r--r-- 1 teo teo 1406 Apr  9 16:02 index.php

Then I added a package ( maximebf/debugbar ) with Composer and I found that the only way to make it run was including its assets in the webroot. 然后,我在Composer中添加了一个软件包( maximebf / debugbar ),我发现使它运行的唯一方法是将其资产包括在webroot中。

So I added a link to vendor into app/webroot : 所以我在app/webroot添加了一个到vendor的链接:

drwxrwxr-x 6 teo teo 4096 Apr  7 19:27 assets
-rw-r--r-- 1 teo teo 1406 Apr  9 16:02 index.php
lrwxrwxrwx 1 teo teo   13 Apr  7 23:49 vendor -> ../../vendor/

I thought: if someone can access the source code of open source packages, it won't hurt... 我以为:如果有人可以访问开源软件包的源代码,那不会有伤害...


Then I started writing my first class, but I can't figure out where I should put the code: 然后我开始写我的第一堂课,但是我不知道应该把代码放在哪里:

  • if I put it in a subfolder of app/ (where I planned to place it), Composer won't insert it's namespace in the autoloader; 如果将其放在app/的子文件夹中(我计划将其放置在其中),Composer不会在自动加载器中插入其名称空间;
  • if I put it in a vendor/ subfolder, it will be linked inside the webroot. 如果我将其放在vendor/子文件夹中,它将被链接到webroot内部。

I'm sure I'm missing something, but I can't figure what... 我确定我想念什么,但我不知道是什么...

In your public web folder leave your system entry file like index.php for example and your resources like CSS, JS etc. 在您的公共Web文件夹中,保留系统入口文件(例如index.php)和资源(例如CSS,JS等)。

On parent folder keep your vendor/ and your app/(your classes) folders. 在父文件夹上,保留您的供应商/和您的应用程序/(您的课程)文件夹。

In vendor/ you must not put anything, this folder is auto generated from composer, so it's possible when you update composer all of your code written in vendor to be overwritten, don't modify anything in this folder. 在vendor /中,您不能放任何东西,该文件夹是由composer自动生成的,因此当您更新composer时,写在卖方中的所有代码都可能被覆盖,请不要修改此文件夹中的任何内容。

So your directory structure must look something like: 因此,您的目录结构必须类似于:

  • app/ 应用程序/
  • vendor/ 供应商/
  • web/ 网络/
  • composer.json composer.json
  • ... ...

Now composer gives you an autoloader to use. 现在,composer为您提供了自动装带器以供使用。 So how to use it? 那么如何使用呢?

In your composer.json file: 在您的composer.json文件中:

{
    "require": {
        "example/example": "~1.0"
    },
    "autoload" : {
        "psr-4": {
          "YourAppNamespace\\": "app"
        }
    }
}

So now lets open for example 所以现在让我们打开

app/controller/myclass.php
app/model/mymodel.php

Code: 码:

<?php 

namespace YourAppNamespace\controller;

use example\example\something; // vendor/example/example/something.php
use YourAppNamespace\model\mymodel; // app/model/mymodel.php

Composer supports multiple loading of namespaces. Composer支持多种名称空间加载。 More about the topic you can find here: https://getcomposer.org/doc/01-basic-usage.md#autoloading 您可以在这里找到有关该主题的更多信息: https : //getcomposer.org/doc/01-basic-usage.md#autoloading

Don't forget when you change the autoload section in the composer.json file to run 不要忘记更改composer.json文件中的autoload部分以使其运行

php composer.phar dump-autoload

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

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