简体   繁体   中英

Composer vendor/ path

As stated anywhere on the web, a webroot folder should only contain an entry point (index.php) and the assets folder. 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 :

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.

So I added a link to vendor into app/webroot :

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;
  • if I put it in a vendor/ subfolder, it will be linked inside the 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.

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.

So your directory structure must look something like:

  • app/
  • vendor/
  • web/
  • composer.json
  • ...

Now composer gives you an autoloader to use. So how to use it?

In your composer.json file:

{
    "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. More about the topic you can find here: 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

php composer.phar dump-autoload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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