Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - How do I AutoLoad a custom class?

Tags:

php

yii2

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

@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.

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

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

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

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.

SOLUTION

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

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

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

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

//namespace common\components\helper;
class CustomDateTime{function Now() {...}}
like image 300
zDaniels Avatar asked Jan 30 '15 02:01

zDaniels


People also ask

What is the use of autoload in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is URL rule?

A URL rule is a class implementing the yii\web\UrlRuleInterface, usually yii\web\UrlRule. Each URL rule consists of a pattern used for matching the path info part of URLs, a route, and a few query parameters. A URL rule can be used to parse a request if its pattern matches the requested URL.

What is composer in Yii?

Composer is a tool for dependency management in PHP. Yii2 uses it to install itself and other vendors' modules (for example, bootstrap). It is also possible to install Yii2 in the old way, by downloading the complete package and transferring it to the host, local or remote, where the framework will be installed.


1 Answers

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

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

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

like image 134
Neeraj Kumar Avatar answered Oct 05 '22 17:10

Neeraj Kumar