5

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 ?

Julien Malige
  • 3,295
  • 1
  • 20
  • 39
  • 1
    You have folders `app` and `controller`, not `App` and `Controller` like in your namespace. – Maerlyn Jul 01 '15 at 17:43
  • I had think about it, but I'm not sure that is a link between namespace and folder name – Julien Malige Jul 01 '15 at 17:47
  • 1
    What OS are you on? On linux, the file systems are case-sensitive, thus app and App are different directories. – Maerlyn Jul 01 '15 at 20:30
  • 1
    Your RewriteBase is wrong, rewritebase is relative to URI not filesystem (well, also filesystem but depending on your webroot folder). [Take a look at this](http://stackoverflow.com/questions/21347768/what-does-rewritebase-do-and-how-to-use-it). PS: Make sure that your webroot is the *web* folder, not *myfolder* folder, otherwise you're making all your project files directly accessible from anywhere – mTorres Jul 02 '15 at 05:45
  • @mTorres : I'm on a server with multiple projects, so that's why I use myfolder (a folder for each project). In this folder, I have my silex project. I think as you that the problem is in the .htaccess or in the server configuration. "PS: Make sure that your webroot is the web folder" with a vhost or an superior htaccess ? – Julien Malige Jul 02 '15 at 09:44
  • 1
    yes, if your webroot is *myfolder* folder, then you definetely need an htaccess to forbid the acces to all non public folders. – mTorres Jul 02 '15 at 12:32
  • It's ok, I have a htaccess in myfolder wich is pointing in the web folder. And in my web/ folder I have a htaccess wich is making the silex job (pointing on the index) – Julien Malige Jul 02 '15 at 12:34

1 Answers1

4

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

Julien Malige
  • 3,295
  • 1
  • 20
  • 39