1

I have a custom asset bundle defined in my plugin. My plugin has a controller. The controller has a action:

/**
 * Handle a request going to our plugin's index action URL,
 * e.g.: actions/entries-by-group/entries-by-group
 *
 * @return mixed
 * @throws \yii\base\InvalidConfigException
 */
public function actionIndex()
{
    $this->view->registerAssetBundle(EntriesByGroupAssets::class);
    $result = 'Welcome to the EntriesByGroupController actionIndex() method';

    return $result;
}

As you can see, I am trying to register my asset bundle. I am getting the error:

ReflectionException

Class recoveryfinder\entriesbygroup\assets\EntriesByGroupAssets does not exist

My namespaces are:

namespace recoveryfinder\entriesbygroup\controller;

or in the asset bundle file:

namespace recoveryfinder\entriesbygroup\assets;

My plugin is in a folder called (note the hyphens):

vendor\recoveryfinder\entries-by-group\src\controllers\EntriesByGroupController.php

It seems that when code gets to ClassLoader::LoadClass I get the error referenced above - class does not exist. It looks to me like ClassLoader is using the namespace as the path spec (without the hyphens) rather than the actual location (with the hyphens).

I must need to set paths in my plugin? If not what is wrong here?

2

The autoloader won't load files in the folder assets as already stated it is all explained here. Your asset bundles should be in a folder called resources otherwise Yii won't register them in its classmap automatically.

Your namespaces must represent your folder structure. The namespace ns\prefix\controller must be for a file in vendor\ns\prefix\src\controller so your namespace ns\prefix\assets will search for a file in ...vendor\ns\prefix\src\assets but you have to include the resource on your own via require_once since Yii won't so it because you are in the wrong folder

The easiest solution would be to create a folder resources and use the namespace ns\prefix\src\resources

Or you can - like stated in the docs - use your plugin root path as well recoveryfinder\entriesbygroup

  • Thanks @Robin Schambach. Here is the PSR-4 autoloader spec. Also, I just read the Yii docs on modules. It looks like classes in Yii and by extension Craft autoload as long as you follow certain rules. – Eric Snyder May 13 '18 at 17:06
  • I know. That's why I told you to restructure your files because they won't be loaded automatically if you don't follow these rules. I'm not sure what you are trying to say. Only specific folders will be loaded automatically... Not all of them – Robin Schambach May 13 '18 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.