简体   繁体   中英

In yii2, how do I autoload my own PHP classes?

Inside my Controller I want a function to use mpdf eg

public function actionPdf(){

    include("MPDF57/mpdf.php");
    $mpdf=new mPDF('c'); 
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->WriteHTML("<h1>Hello World!</h1>");
    $mpdf->Output('filename.pdf', 'F');

    }

}

This does not work, and throws an error:

Class 'app\\controllers\\mPDF' not found

What should I do If I want to autoload the class

(a). Just for this Controller Action

(b). To make it usable everywhere just by using the use statement.

I know it has to do something with namespaces but don't know how do I define a namespace, and where do I place this MPDF57 folder and then make it accessible.

I also tried this :

 $name = "MPDF57/mpdf.php";
spl_autoload_register(function ($name) {
    var_dump($name);
});

But this didn't work either. throws the same error when I call my controller Action.

Here is the namespace declaration and use statements inside :

namespace app\controllers;

    use Yii;
    use app\models\Regs;
    use app\models\Voters;
    use app\models\RegsSearch;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    use \yii\web\Response;
    use yii\helpers\Html;
    use kartik\mpdf\Pdf;

Yii has already had autoloader, you do need nothing to load your class. Just create your class with correct namespace and it will be loaded where are you using it only.

Namspace should represent real path to PHP file. PHP file name and class name should be same.

You should simply use mpdf/mpdf package :

  1. Install it using composer :

    composer require "mpdf/mpdf" ">=6.0.0"

  2. Use it like this :

    $mpdf = new \\mPDF();

Or you can use a yii2 extension like this one : https://github.com/kartik-v/yii2-mpdf

I've faced such problems in one of my previous projects. I'm not good at PHP or Yii2 - so follow my guide on your own risk :)

When you you add use path\\to\\ExternalLibrary that means the interface is ready to use inside current class (eg CurrentController.php).

That means your application knows how to bring your path to it's stage.

Eg use common\\models\\Post lets you directly to use Post class, as $posts = new Post;

So if your library contains only one file, just put is some "canonic" path. To common\\models\\ for example. So you can use it like any other model interface.

But for sake of your project put it on vendor folder. Then install a random library with composer. And observe which files are modified (1-3 generally). Also try to understand the modification logic. When you get sure that you've grasped everything, copy and paste these parts and change the paths, names, etc. for your library.

The best way, I think, is to make your library PSR-4 compatible and ship it as a PHP package. Thus, others can also benefit from your work.

There are lots of guides about making php packages.

If you are planning to be a good PHP developer, I recommend to look up Josh Lockhart's "Modern PHP: New Features and Good Practices" book ( free pdfs are available :) ). That will help you to understand the fundamentals of OO PHP including namespaces, interfaces etc. So, you will be able to handle such problems in modern way.

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