简体   繁体   English

Composer自动加载未加载类

[英]Composer Autoload is not loading the Class

I am new to using composer and psr-0. 我是刚开始使用composer和psr-0的人。 I have tried a small app using composer and psr-0. 我尝试了使用作曲家和psr-0的小型应用程序。 I have used namespace to load a particular class. 我已经使用命名空间加载特定的类。 When i call a class using composer vendor/autoload I am getting class not found error. 当我使用作曲家供应商/自动加载程序调用类时,出现类未找到错误。

My composer.json file:/var/www/html/silexapp/composer.json 我的composer.json文件:/var/www/html/silexapp/composer.json

{
"require": {
    "silex/silex": "~2.0",
    "symfony/console": "~2.6"
},
"autoload": {
    "psr-0": {
        "MyApp": "/silexapp/app"
    }
}
}

My composer vendor autoload file: /var/www/html/silexapp/vendor/autoload.php 我的作曲家供应商自动加载文件:/var/www/html/silexapp/vendor/autoload.php

     <?php

      // autoload.php @generated by Composer

     require_once __DIR__ . '/composer' . '/autoload_real.php';

    return ComposerAutoloaderInitf7241d907c173a8d77da0791cc918856::getLoader();

My class file name Underline.php: /var/www/html/silexapp/app/Tnq/Todo/Command/Underline.php 我的班级文件名称Underline.php:/var/www/html/silexapp/app/Tnq/Todo/Command/Underline.php

      <?php 
      namespace MyApp\Tnq\Todo\Command;
     class Underline{

        public function add($a,$b){

        return $result = $a+$b;

     }

   }

  ?>

My another class file name Bold.php: /var/www/html/silexapp/app/Tnq/Todo/Command/Bold.php 我的另一个类文件名Bold.php:/var/www/html/silexapp/app/Tnq/Todo/Command/Bold.php

    <?php
    require_once "../../../../vendor/autoload.php";
    //require_once "Underline.php";

   use MyApp\Tnq\Todo\Command as tool;

   echo "this is the index file to check namespace.";
   $c = new tool\Underline();
   echo "=============================";
   echo "Addition : ".$c->add(2,2);
   ?>

I am getting "class not found error" in my bold.php class file, when I use autoload file. 使用自动加载文件时,我的bold.php类文件中出现“类未找到错误”。 But when I directly included the underline class file, I am getting the output. 但是当我直接包含下划线类文件时,我得到了输出。 Why it is not working when I use autoload? 为什么在使用自动加载功能时不起作用?

Can anyone help me to find the issue? 谁能帮我找到问题?

The "key" should be a directory under the path you put as "value", that should be relative to your working directory. “键”应该是您放置为“值”的路径下的目录,该目录应该对于您的工作目录。 To look at it in a simple way, the namespace should map the directory structure; 为了简单地看待它, namespace应该映射目录结构。 you are missing a MyApp directory. 您缺少MyApp目录。

If in your composer.json have: 如果在composer.json具有:

    "autoload": {
        "psr-0": {
            "MyApp\\": "app/"
        }
    }

Then you need a MyApp directory under app/ . 然后,您需要在app/下有一个MyApp目录。 Try this: 尝试这个:

composer.json: composer.json:

// /var/www/html/silexapp/composer.json
{
    "require": {
        "silex/silex": "~2.0",
        "symfony/console": "~2.6"
    },
    "autoload": {
        "psr-0": {
            "Tnq\\": "app/"
        }
    }
}

Underline.php: Underline.php:

<?php
// /var/www/html/silexapp/app/Tnq/Todo/Command/Underline.php 
namespace Tnq\Todo\Command;

class Underline
{
    public function add($a,$b)
    {
        return $result = $a+$b;
    }
}

Bold.php: Bold.php:

<?php
// /var/www/html/silexapp/app/Tnq/Todo/Command/Bold.php
require_once "../../../../vendor/autoload.php";

use Tnq\Todo\Command as tool;

echo 'this is the index file to check namespace.' . PHP_EOL;
$c = new tool\Underline();
echo "=============================";
echo "Addition : ".$c->add(2,2);

In theory, that should works (not tested :) ) 从理论上讲,这应该有效(未经测试:))


sources: 资料来源:

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

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