I am trying to use composer to autoload my classes in a wordpress plugin.
I wish for my models and controllers to reside within the GD namespace, however I would like to put my models and controllers into their own directories.
I am facing the error message: Fatal error: Class 'GDShortcodeController' not found in /.. .../src/GD/App.php on line 10
directory/file structure
plugins
gd
cache
// location for twig template cache
public
// location for plugin assets
src
controllers
ShortcodeController.php
models
App.php
templates
test.html
vendor
// location for application dependencies installed via composer
composer
autoload_namespaces.php
composer.json
{
"require": {
"twig/twig": "1.*"
},
"autoload": {
"psr-0": {
"GD": ["src/", "src/GD/", "src/GD/controllers/", "src/GD/models/"]
}
}
}
vendor/composer/autoload_namespaces.php
return array(
'Twig_' => array($vendorDir . '/twig/twig/lib'),
'GD' => array($baseDir . '/src', $baseDir . '/src/GD', $baseDir . '/src/GD/controllers', $baseDir . '/src/GD/models'),
);
GD/src/App.php
class App
{
private $plugin_dir;
private $twig;
public function init()
{
$shortcodes = new ShortcodeController;
add_shortcode( 'gd', [ $shortcodes , 'gd'] );
}
}
GD/src/controllers/ShortcodeController.php
<?php namespace GD;
class ShortcodeController
{
// STUFF IN HERE
}
Should i be using autoload classmap instead of psr-0? How does composer deal with namespaced classes when using classmap?
You have
"src/GD/controllers/"
in yourpsr-0
hereBut
ShortcodeController.php
file inGD/src/controllers
folder andnamespace
isSo, your
ShortcodeController.php
should be in thesrc/GD/controllers/gd
folder but right now the path isGD/src/controllers/ShortcodeController.php
andAutoloader
is looking in to"src/GD/controllers/gd/ShortcodeController.php"
folder (including other entries in the array) or you can change"src/GD/controllers/"
to"GD/src/controllers/"
inpsr-0
. Also, you are usingshortcodes = new ShortcodeController;
from root/globalnamespace
so, it could beAccording to
your Class
Should be in
GD/src/controllers/gd
folder.