我的作曲家项目 class 无法加载

My composer project's class won't load

我有一个用于作曲家的 Packagist 包,lwilson/onepage,其中有一个 PHP 文件。该文件有一个 class,其中包含一个函数。我已经在我的 Web 服务器上安装了这个包,并且我有一个应该使用它的 PHP 文件。我知道作曲家自动加载器已正确包含,因为我可以使用服务器上安装的其他作曲家库。我得到以下结果:

Fatal error: Class 'OnePage\OnePage' not found in /home/photox952/public_html/onepage/test_onepage.php on line 6

test_onpepage.php(使用class的文件):

<?php
require '../../vendor/autoload.php';

use OnePage\OnePage;

file_put_contents("index.php",OnePage::OnePage(json_decode(file_get_contents("cfg.json")),__DIR__));
?>

compiler.php(包中的文件):

<?php
namespace OnePage;

// This is licenced under the GNU GENERAL PUBLIC LICENSE.  More info at: https://github.com/LeoWilson-XnD/lwilson_onepage/blob/master/LICENSE

class OnePage{
 public static function OnePage($cfg,$dir){
  $tmplt_u = "<?php if($_REQUEST['type']==\"{TYPE}\"&&$_REQUEST['src']==\"{SRC}\"){header(\"Content-Type: {CT}\"); ?>{DATA}<?php } ?>";
  $tmplt_c = "<?php if($_REQUEST['all']==\"{TYPE}\"){header(\"Content-Type: {CT}\"); ?>{DATA}<?php } ?>";
  $res = "<?php /* This code is generated by the OnePage tool.  Find out more here: https://github.com/LeoWilson-XnD/lwilson_onepage */ ?>";
  $dir.=$cfg['dir'];
  if($cfg['v']==1){
   foreach($cfg['unique'] as $ka => $va){
    foreach($cfg[$ka] as $kb => $vb){
     $u = $tmplt_u;
     $u=str_replace("{TYPE}",explode("/",$ka)[1],$u);$u=str_replace("{SRC}",$kb,$u);$u=str_replace("{CT}",$ka,$u);$u=str_replace("{DATA}",file_get_contents($dir.$vb),$u);
     $res.=$u;
    }
   }
   foreach($cfg['combine'] as $ka => $va){
    $ds = "";
    foreach($cfg[$ka] as $kb => $vb){
     $ds.=file_get_contents($dir.$vb);
    }
    $u = $tmplt_c;
    $u=str_replace("{TYPE}",explode("/",$ka)[1],$u);$u=str_replace("{CT}",$ka,$u);$u=str_replace("{DATA}",file_get_contents($dir.$vb),$u);
    $res.=$u;
   }
   foreach($cfg['links'] as $key => $val){
    $res = str_replace($val,$key,$res);
   }
  }
  return $res;
 }
}
?>

composer.json(包的 composer.json 文件):

{
    "name": "lwilson/onepage",
    "description": "This allows users to compile websites easily into one PHP file.",
    "authors": [
        {
            "name": "Leo Wilson",
            "email": "lwilson@xlww.net",
   "homepage": "https://xlww.net/",
   "role": "Developer"
        }
    ],
    "minimum-stability": "stable",
 "version": "v1.0.0",
 "homepage": "https://github.com/LeoWilson-XnD/lwilson_onepage",
 "licence":"GPL-3.0",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "": "/"
        }
    }
}

如果我遗漏了一些明显的东西,我提前道歉。

我认为你在 composer 中设置了错误的自动加载。尝试创建一个名为 OnePage 的文件夹,并将带有 class 的文件放入其中。必须命名为 OnePage.php

然后像这样设置自动加载

  "autoload": {
    "psr-4": {
      "OnePage\": "OnePage"
    }
  }

然后 运行 composer update 来自 shell。然后再次尝试 运行 更新您的 php 文件。

OnePage.php 文件应该在文件夹 /home/photox952/public_html/onepage/OnePage