Block controller can't find a class

Permalink
Hi!

I've been building a custom Block Type and facing a problem at the moment.
This is a simple block that has only a textarea for text to be preprocessed and stored in database.
I want to use a third party class for preprocessing, so I created a src folder at the root of my block and placed that helper class there.
I try to use this class in block's controller.php save() method:
$helper = new CustomTextHelper();

but getting an error in modal window:

"An unexpected error occurred. Class 'Application\Block\MyBlock\Src\CustomTextHelper' not found"

My block is inside application/blocks/my_block directory, namespace and import is set as it should be.

Have to note that block is working when I just store the text without using preprocessing class.

Am I doing something wrong? Is it some kind of issue with auloloading?

Thanks in advance!

Quicksilver
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi Quicksilver,

When you say "third party class", do you mean a third party library or a custom class?

I don't think src inside a block will be autoloaded.

For a custom class try this:

File path:
application\src\CustomTextHelper.php

CustomTextHelper.php namespace and class:
<?php
namespace Application\Src;
class CustomTextHelper
{
    // class code
}

To use the custom class in your block controller:
- add this under the controller namespace
use Application\Src\CustomTextHelper;

- to instantiate your class
$customTextHelper = new CustomTextHelper();
Quicksilver replied on at Permalink Reply
Quicksilver
Yes, I meant custom class which I wrote to use within this block.

I tried your advice about placing the class inside application/src and it worked! Thank you!

Is there any way to use it when it's inside block's folder? Except of require() of course.
MrKDilkington replied on at Permalink Reply
MrKDilkington
I am not aware of src being used inside a block. Someone else might have more information regarding that.

An alternative is to create a package for the custom class and block.
Quicksilver replied on at Permalink Reply
Quicksilver
Thank you for your help!