php Yii2 - 如何自动加载自定义类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28228407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:41  来源:igfitidea点击:

Yii2 - How do I AutoLoad a custom class?

phpyii2

提问by zDaniels

I have created the following custom class that I'd like to use in my Yii2 application:

我创建了以下我想在 Yii2 应用程序中使用的自定义类:

@common/components/helper/CustomDateTime.php

@common/components/helper/CustomDateTime.php

namespace common\components\helper;
class CustomDateTime{function Now() {...}}

I want to use this class like this:

我想像这样使用这个类:

public function actionDelete($id)
{
    $account = $this->findModel($id);
    $account->archived = 1;
    $account->archived_date = CustomDateTime::Now();
    $account->save();

    return $this->redirect(['index']);
}

In my @common/config/bootstrap.php file I've created a classMap according to this http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html.

在我的 @common/config/bootstrap.php 文件中,我根据这个http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html创建了一个 classMap 。

Yii::$classMap['CustomDateTime'] = '@common/components/helper/CustomDateTime.php';

But I am getting the error: Class 'app\controllers\myapp\CustomDateTime' not found

但我收到错误:找不到 Class 'app\controllers\myapp\CustomDateTime'

QUESTION:How do I create a classMap so that I don't have to use the usestatement at the beginning of every controller to access my custom class?

问题:如何创建 classMap 以便不必在每个控制器的开头使用 use语句来访问我的自定义类?

Yii 1.1 used to have an option in the config file to 'import' a set of code so that a class file could be autoloaded when it was called.

Yii 1.1 曾经在配置文件中有一个选项来“导入”一组代码,以便在调用时可以自动加载类文件。

SOLUTION

解决方案

Many thanks to @Animir for redirecting me back to the original documentation. http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html.

非常感谢@Animir 将我重定向回原始文档。 http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html

I found that I can use the following in my @common/config/bootstrap.phpfile

我发现我可以在我的@common/config/bootstrap.php文件中使用以下内容

Yii::$classMap['CustomDateTime'] = '@common/components/helper/CustomDateTime.php';

BUT - it only works when the the CustomDateTime.php file does NOT have a declared namespace.

但是 - 它仅在 CustomDateTime.php 文件没有声明的命名空间时才有效。

//namespace common\components\helper;
class CustomDateTime{function Now() {...}}

回答by Animir

Just add usestatement in file, where you use a class.

只需use在文件中添加语句,在其中使用类。

use common\components\helper\CustomDateTime;
/* some code */
public function actionDelete($id)
{
    $dateNow = CustomDateTime::Now();
    /* some code */
}
/* some code */

回答by SohelAhmedM

You can do thisway too without the use of use

你可以做这样的方式太不使用use

Yii::$app->cdt->Now(); // cdt - CustomDateTime

Yii::$app->cdt->Now(); // cdt - CustomDateTime

but for this thing to get accomplished successfully you add below to app/config/web.php(the config file)

但是为了成功完成这件事,您将下面添加到app/config/web.php(配置文件)

...
'components' => [ 
    'cdt' => [
        'class' => 'common\components\helper\CustomDateTime',
    ],
    ...
]
...

回答by Mir Adnan

I know it's been long since this question was asked. I came across this and to share what I do.

我知道这个问题已经很久了。我遇到了这个并分享我的工作。

I usually use an alias after usestatement instead of adding 100 of lines of use statements for each model or class objects. Adding classMap for many of the classes that you use is not a good idea in my view. If you do that you'll be just adding unnecessary mapping even when you are not using those objects.

我通常在use语句之后使用别名,而不是为每个模型或类对象添加 100 行 use 语句。在我看来,为您使用的许多类添加 classMap 并不是一个好主意。如果您这样做,即使您不使用这些对象,您也只会添加不必要的映射。

Here's what I do

这就是我所做的

use Yii;
use common\helpers as Helper;
use yii\helpers as YiiHelper;
use common\models as Model;
use common\components as Component;

And in the controller

并在控制器中

public function actionIndex(){
   $model = Model\SomeModel();

   if(Yii::$app->request->isPost){
       $post = Yii::$app->request->post('SomeModel');
       $model->text = Helper\Text::clean($post['text']);
       $model->text = YiiHelper\Html::encode($model->text);

       if($model->save()){
           return $this->render('thank_you');
       }
   }
}

Hope this helps :)

希望这可以帮助 :)

回答by Neeraj Kumar

Auto load in Yii 2 is pretty easy. You can use it by loading the class with config main file. like

Yii 2 中的自动加载非常简单。您可以通过使用配置主文件加载类来使用它。喜欢

You have created your class in components/helper/CustomDateTime.php . and now in your config/main.php in the top of the file add below code

您已经在 components/helper/CustomDateTime.php 中创建了您的类。现在在文件顶部的 config/main.php 中添加以下代码

require_once( dirname(__FILE__) . '/../components/helper/CustomDateTime.php');

Now your class is autoloaded in your project where ever you want to use it you can use like in your controller,model or view

现在您的类会自动加载到您的项目中,您可以在任何您想使用它的地方使用它,例如在您的控制器、模型或视图中使用

Simply use like this

只需像这样使用

CustomDateTime-><methodName>();

Try this i have used this in my project.

试试这个我在我的项目中使用过这个。

For refrence you can use this link. click here

作为参考,您可以使用此链接。点击这里