"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:
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:
<?php namespace Concrete\Package\MsmLsCache\Src;
defined('C5_EXECUTE') or die("Access Denied.");
use Config;
class LsCache 
{
   // protected static $packageHandle = 'msm_ls_cache';
    public static function get($key)
    {
        Log::AddEntry('on start');
    }
}


No matter what I do it won't find the class!

What do I need to check?

Dave

madesimplemedia
 
hutman replied on at Permalink Best Answer Reply
hutman
Do you have something like this in your package controller

protected $pkgAutoloaderRegistries = array(
   'src' => '\Concrete\Package\MsmLsCache\Src'
);
madesimplemedia replied on at Permalink Reply
madesimplemedia
Thanks that's fixed it. :)
JohntheFish replied on at Permalink Reply
JohntheFish
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\'
madesimplemedia replied on at Permalink Reply
madesimplemedia
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
mnakalay replied on at Permalink Reply
mnakalay
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
JohntheFish replied on at Permalink Reply
JohntheFish
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
/*
* 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.
madesimplemedia replied on at Permalink Reply
madesimplemedia
I have definitely declared v8 thanks.

I will give that a go cheers.