"Class not found" in custom package
Permalink
Hi,
I'm using v8. Really stumped by this, I'm getting "Class not found" in my packages controller when the class is clearly there!
controller.php:
And in my src directory I have LsCache.php:
No matter what I do it won't find the class!
What do I need to check?
Dave
I'm using v8. Really stumped by this, I'm getting "Class not found" in my packages controller when the class is clearly there!
controller.php:
use Concrete\Package\MsmLsCache\Src\LsCache; public function on_start() { Events::addListener('on_before_render', function ($event) { $cOn = LsCache::get('msm_ls_cache.settings.cache_on'); }); }
And in my src directory I have LsCache.php:
No matter what I do it won't find the class!
What do I need to check?
Dave
Do you have something like this in your package controller
Thanks that's fixed it. :)
What that does is map the namespace that used to be provided by 5.7 into the same namespace in v8.
As a developer, I find it more robust to have my own namespace. It keeps everything clear of the core namespace, other packages, and doctrine elements. (afaik, that namespace was deprecated in v8 to avoid ambiguities between such. See
https://documentation.concrete5.org/developers/packages/adding-custo... )
eg
'src/my_classes/' => '\MyClasses\'
As a developer, I find it more robust to have my own namespace. It keeps everything clear of the core namespace, other packages, and doctrine elements. (afaik, that namespace was deprecated in v8 to avoid ambiguities between such. See
https://documentation.concrete5.org/developers/packages/adding-custo... )
eg
'src/my_classes/' => '\MyClasses\'
Hi John, I see your point but I can't get that to work, I get an error about File Mapping.
My class file is \packages\msm_ls_cache\src\LsCache.php
The package namespace is Concrete\Package\MsmLsCache
My class file is \packages\msm_ls_cache\src\LsCache.php
The package namespace is Concrete\Package\MsmLsCache
You have to make sure your package declares a minimum version of 8.x.x. If you declare 5.7.x C5 will consider you're using legacy code and do stuff differently
That array of paths to namespaces is already inside your package. You don't need the package path. Also, you don't need the full class, just the namespace. Than namespace needs to match the namespace in the class php file.
example
This also works with c5.7, but was not the standard practice because of the automatic namespace that is now deprecated.
Looking at my post above, perhaps the trailing '\' was misleading or wrong. The double backslashes depend on the type of string quote. I am just in the habit of always double backslashing, whatever the string.
example
/* * In package controller */ array( 'src/JtF/PackageMagic' => '\\JtF\\PackageMagic' );
/* * file packages/jl_package_magic_starter/src/JtF/PackageMagic/ExtendedPackageArchive.php */ namespace JtF\PackageMagic; // some lines of use .... etc left out class ExtendedPackageArchive extends PackageArchive
This also works with c5.7, but was not the standard practice because of the automatic namespace that is now deprecated.
Looking at my post above, perhaps the trailing '\' was misleading or wrong. The double backslashes depend on the type of string quote. I am just in the habit of always double backslashing, whatever the string.
I have definitely declared v8 thanks.
I will give that a go cheers.
I will give that a go cheers.