Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex 'App\Controller\IndexController' not found on server

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 ?

like image 341
Julien Malige Avatar asked Sep 28 '22 11:09

Julien Malige


1 Answers

@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.

like image 81
Julien Malige Avatar answered Oct 03 '22 02:10

Julien Malige