My silex project works on local, but when I put it online, I have a NameSpace or Class resolution problem :
Fatal error: Class 'App\Controller\IndexController' not found in /homepages/40/d453499750/htdocs/myfolder/app/bootstrap.php on line 19
Structure :
/
->myfolder
->app
->controller
->IndexController.php
->bootstrap.php
->...
->vendor
->web
->.htaccess
-> index.php
-> ...
composer.json
{
"minimum-stability":"dev",
"autoload": { "psr-0": { "App\\": "./" }},
"require":{
"silex/silex": "~1.2",
"symfony/twig-bridge":"2.1.*",
"twig/twig":">=1.8,<2.0-dev"
}
}
.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /myfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [QSA,L]
</IfModule>
boostrap.php
<?php
use App\Controller\IndexController;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\Provider;
/* load vendors */
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
/* unable path() and url() */
$app->register(new UrlGeneratorServiceProvider());
/* twig */
$app->register(new TwigServiceProvider());
/* load the controllers*/
$loader->add("App",dirname(__DIR__));
$app->mount("/", new IndexController());
return $app;
IndexController.php
namespace App\Controller {
use Silex\Application;
use Silex\ControllerProviderInterface;
class IndexController implements ControllerProviderInterface
{
...
My silex is in a folder but I rewriteBase in .htaccess. What I'm doing wrong ?
@Maerlyn "You have folders app and controller, not App and Controller like in your namespace."
This made me on the right track !
I have renamed my folders with first letter uppercase and it was worked. But I thought about the Silex vendor wich was working with uppercase namespace and lowercase dirnames...
The solution is in the composer.json and more precisely in the autoload parameter.
With this functionality, you can map your namespaces and your folder :
"autoload": {
"psr-4": {
"App\\Controller\\": "./app/controller"
}
},
Here you can find more information :
https://getcomposer.org/doc/01-basic-usage.md#autoloading
and you can find generated code in the vendor => composer => autoload_psr4 file
return array(
...
'App\\Controller\\' => array($baseDir . '/app/controller'),
);
With this I can keep lower case folders (as silex default), and uppercase namespaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With