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:
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!
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!
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.
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.
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.
An alternative is to create a package for the custom class and block.
Thank you for your help!
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:
To use the custom class in your block controller:
- add this under the controller namespace
use Application\Src\CustomTextHelper;
- to instantiate your class