cakephp/cakephp · error · InvalidArgumentException
Class ` ` does not exist or does not extend…
Error message
Class `%s` does not exist or does not extend `Cake\Core\PluginInterface`.
What it means
PluginCollection::create() accepts FQCNs containing a backslash. If the class exists as a string but is not a subclass of Cake\Core\PluginInterface, it throws InvalidArgumentException because it cannot instantiate a valid plugin from it.
Solutions
- Pass the plugin name (e.g. 'DebugKit') instead of a class name, letting Cake resolve it
- Ensure the referenced class extends Cake\Core\BasePlugin (implements PluginInterface) and the namespace/autoload mapping is correct
- Run composer dump-autoload so the class can be found
Example fix
// before
$plugin = $plugins->create('App\Application');
// after
$plugin = $plugins->create('MyCompany\MyPlugin\MyPluginPlugin'); // class extends BasePlugin Defensive patterns
Strategy: try-catch
Validate before calling
if (str_contains($name, '\\') && !is_subclass_of($name, \Cake\Core\PluginInterface::class)) {
throw new \InvalidArgumentException("$name is not a PluginInterface");
} Try / catch
try {
$plugins->create($name);
} catch (\InvalidArgumentException $e) {
// fall back to treating $name as a plugin name, not a class
} Prevention
- Pass short plugin names unless instantiating a custom class
- Ensure custom plugin classes extend Cake\Core\BasePlugin
- Run composer dump-autoload after class renames
When it happens
Trigger: Passing an app class name like 'App\Application' or a typo'd class that does not implement PluginInterface to create()/get().
Common situations: Using the Application class name as a plugin name; referencing a plugin class that was renamed when upgrading from Cake 3 to Cake 4/5 (BasePlugin introduction); namespace typos.
Related errors
- Behavior class ` ` could not be found.
- Cannot find route class
- Cannot load ` ` for use in integration testing.
- class
- Datasource class ` ` could not be found.
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/d3c7adfe108d6728.
Report an issue: GitHub.
Appendix: source
Thrown at src/Core/PluginCollection.php:265
/**
* Create a plugin instance from a name/classname and configuration.
*
* @param class-string<\Cake\Core\PluginInterface>|string $name The plugin name or classname
* @param array<string, mixed> $config Configuration options for the plugin.
* @return \Cake\Core\PluginInterface
* @throws \Cake\Core\Exception\MissingPluginException When plugin instance could not be created.
* @throws \InvalidArgumentException When class name cannot be found or an empty name is provided.
*/
public function create(string $name, array $config = []): PluginInterface
{
if ($name === '') {
throw new InvalidArgumentException('Plugin name cannot be empty.');
}
if (str_contains($name, '\\')) {
if (!is_subclass_of($name, PluginInterface::class)) {
throw new InvalidArgumentException(sprintf(
'Class `%s` does not exist or does not extend `Cake\Core\PluginInterface`.',
$name,
));
}
return new $name($config);
}
$config += ['name' => $name];
$namespace = str_replace('/', '\\', $name);
$pos = strpos($name, '/');
$namePart = $pos === false ? $name : substr($name, $pos + 1);
// Check for [Vendor/]Foo/FooPlugin class
$className = $namespace . '\\' . $namePart . 'Plugin';
if (!class_exists($className)) {View on GitHub (pinned to 1128eba9b0)