register namespace in silex without composer
-
26-12-2019 - |
Question
I'm using silex to build an api for an app I'm building, and I want to abstract my controllers into different files.
I work for a pretty big corporation with a very strict IT department, and they won't let me install composer. For this reason, I need to register namespaces manually, via the $loader->add()
function, but I get the following error:
Fatal error: Class 'App\Controller\SessionController' not found in C:\path\to\webroot\App\bootstrap.php on line 9
Does anyone know why the autoloader isn't picking up my App namespace?
Currently, my app is structured as follows:
App/
Controller/
ApiControllerAbstract.php
SessionController.php
bootstrap.php
vendor/
www/
index.php
index.php
require_once "../app/bootstrap.php";
bootstrap.php
$loader = require_once __DIR__.'/../vendor/autoload.php';
$loader->add('App', __DIR__ . '/../App/');
$app = new Silex\Application();
$app->mount('/session', new App\Controller\SessionController());
$app->run();
SessionController.php
namespace App\Controller;
class SessionController extends ApiControllerAbstract {
public function connect( $app)
{
$this->app = $app;
$controller_collection = $app['controllers_factory'];
// DECLARE ALL YOUR ROUTES HERE
//...
}
}
Solution
Hello, The following line:
$loader->add('App', __DIR__ . '/../App/');
Should be:
$loader->add('App', __DIR__ . '/../');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow