Adrieng
January 26, 2024, 10:59am
1
Hi there,
I’m starting to create my first custom panel menu. I just copied-pasted the sample code from the reference to my config and I’ve got an error : Class "App" not found
.
I’m using version 4.0.3, installed through composer.
Here is my full config.php :
<?php
return [
'debug' => true,
'panel' => [
'menu' => [
'site' => [
'label' => 'Overview'
],
'-',
'notes' => [
'icon' => 'pen',
'label' => 'Notes',
'link' => 'pages/notes',
'current' => function ($current) {
$path = App::instance()->request()->path()->toString();
return Str::contains($path, 'pages/notes');
}
],
'users'
]
]
];
If you want to use the App
class directly you have to include the namespace in your file.
use Kirby\Cms\App;
App::instance()->request();
// Or use the alias
Kirby::instance()->request();
// Or use the helper function
kirby()->request();
texnixe
January 26, 2024, 11:24am
3
You need to import the class or use the Fully Qualified name::
Kirby\Cms\App
Adrieng
January 26, 2024, 11:31am
4
Thank you !
Why it isn’t necessary for the Kirby
class to be imported ?
Why the sample from the reference is using the App
class instead of the Kirby
one ?
texnixe
January 26, 2024, 11:40am
5
Kirby is not a class name but a class alias that is made available globally.
1 Like
The Kirby
alias is registered without a namespace. So the fully qualified name is simply Kirby
.
<?php
return [
// cms classes
'collection' => 'Kirby\Cms\Collection',
'file' => 'Kirby\Cms\File',
'files' => 'Kirby\Cms\Files',
'find' => 'Kirby\Cms\Find',
'helpers' => 'Kirby\Cms\Helpers',
'html' => 'Kirby\Cms\Html',
'kirby' => 'Kirby\Cms\App',
'page' => 'Kirby\Cms\Page',
'pages' => 'Kirby\Cms\Pages',
'pagination' => 'Kirby\Cms\Pagination',
'r' => 'Kirby\Cms\R',
'response' => 'Kirby\Cms\Response',
's' => 'Kirby\Cms\S',
'sane' => 'Kirby\Sane\Sane',
'site' => 'Kirby\Cms\Site',
'structure' => 'Kirby\Cms\Structure',
'url' => 'Kirby\Cms\Url',
1 Like
texnixe
January 26, 2024, 11:43am
7
Sorry, missed that you beat me to it, @lukaskleinschmidt
No worries
Regarding the usage in the example. It would probably make sense to either include the use
statement or use one of the other two ways to get the kirby instane. The v4 docs are still pretty new and especially the menu option was not available in v3. So not many people came across this issue yet I guess.
texnixe
January 26, 2024, 11:48am
9
I already fixed the page in the docs. Need to check if we are using it in other places as well.
1 Like
texnixe
January 26, 2024, 12:00pm
10
On a side note: An IDE should usually warn you and offer to import undefined classes (and other issues). In PHPStorm, for example, I get this warning:
Adrieng
January 26, 2024, 12:30pm
11
I understand. Thank you both.