类'Dompdf\Dompdf'未找到

3
我将尝试使用composer安装dompdf,我按照使用Composer安装DOMPDF的说明进行操作。
到目前为止,我已经:
  • In composer.json

    ...
    "require": {
        ...
        "dompdf/dompdf": "~0.6.1"
    },
    "autoload": {
    ....
    
  • run composer update

  • in autoload.php already have require __DIR__.'/../vendor/autoload.php';
  • In vendor/dompdf/dompdf/dompdf_config.inc.php changed def("DOMPDF_ENABLE_AUTOLOAD", true); to def("DOMPDF_ENABLE_AUTOLOAD", false);
  • My controller code

```

use Dompdf\Adapter\CPDF;      
use Dompdf\Dompdf;
use Dompdf\Exception;

require_once "vendor/dompdf/dompdf/dompdf_config.inc.php";

class ArticleController extends BaseController {
  ...
  public function downloadPdf(){
    $dompdf = new Dompdf();
    $dompdf->loadHtml('hello world');
    $dompdf->render();
    $dompdf->output();
  }
}
  • "post" route for ArticleController@downloadPdf

so now when I try to download pdf, if gives me error:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Dompdf\Dompdf' not found'

have I missed any setup step or doing something wrong?

3个回答

1

我认为你不想使用dompdf 0.6 family。在0.6版本中,所有类都在全局空间中。但由于你的代码已经准备好使用0.7版,所以请将其更改为

"dompdf/dompdf": "~0.7"

运行 composer update


更新dompdf版本会有所帮助,但在他们的github页面上显示v0.6.2为稳定版本,所以我不想升级到0.7,感谢您的回复。 - RHM
@RHM,删除 use Dompdf \ Dompdf;。就像我说的,所有类都在全局空间中。$dompdf = new \Dompdf(); - Federkun
我尝试过了@Federico,但是在0.6版本中我无法使用Dompdf或\Dompdf,因为它没有命名空间。 - RHM

1

这个dompdf github页面上的问题帮助我解决了这个错误。

最新稳定版(0.6.1)不支持命名空间,因此您的代码中不需要使用use语句。即将发布的版本(0.7.0)包括命名空间支持。

所以,我只是移除了

use Dompdf\Adapter\CPDF;      
use Dompdf\Dompdf;
use Dompdf\Exception;

现在使用 new DOMPDF(); 而不是 new Dompdf();,因为版本 0.6.* 不支持命名空间。


1
打开config.php文件。
application/config/config.php

然后更改。
$config['composer_autoload'] = "FALSE";

$config['composer_autoload'] = "vendor/autoload.php";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接