Implementing namespaces in fatfree framework

fat-free-framework, namespaces, php

Solution

The autoloader of the Fat-Free Framework is very basic. It expects you to define one or more autoloading folders, each of them will map to the root namespace.

So let's say you define `$f3->set('AUTOLOAD','app/;inc/')` and your file structure is:

- app
- inc
- lib
  |- base.php
- index.php

Then a class named `MyClass` belonging to the `Foo\Bar` namespace (full path: `Foo\Bar\MyClass`) should be stored in either `app/foo/bar/myclass.php` or `inc/foo/bar/myclass.php` (remember: we specified two autoloading folders).

NB: don't forget to specify `namespace Foo\Bar` at the beginning of `myclass.php` (the autoloader won't do it for you).

--

So to answer your specific problem, having the following file structure:

- lib
  |- base.php
- src
  |- controllers
     |- index.php
- index.php

three configurations are possible:

Config 1

`$f3->set('AUTOLOAD','src/controllers/')`

Then all the files under `src/controllers/` will be autoloaded, but remember : `src/controllers/` maps to the root namespace, so that means the `Index` class should belong to the root namespace (full path: `\Index`).

Config 2

`$f3->set('AUTOLOAD','src/')`

Then all the files under `src/` will be autoloaded, which means the `Index` class should belong to the `Controllers` namespace (full path: `\Controllers\Index`).

Config 3

`$f3->set('AUTOLOAD','./')`

Then all the files under `./` will be autoloaded, which means the `Index` class should belong to the `Src\Controllers` namespace (full path: `\Src\Controllers\Index`).

Problem

I am trying to use namespaces in fatfree framework, but somehow its not able to find the class following is my setup routes.ini ``` [routes] GET /=Src\Controllers\Index->index ``` index.php ``` namespace Src\Controllers; class Index { function index($f3) { $f3->set('name','world'); echo View::instance()->render('template.htm'); } } ``` Global index.php ``` // Retrieve instance of the framework $f3=require('lib/base.php'); // Initialize CMS $f3->config('config/config.ini'); // Define routes $f3->config('config/routes.ini'); // Execute application $f3->run(); ``` UPDATE: Error: Not Found HTTP 404 (GET /) • index.php:13 Base->run() UPDATE 2: config.ini ``` [globals] ; Where the framework autoloader will look for app files AUTOLOAD=src/controllers/ ; Remove next line (if you ever plan to put this app in production) DEBUG=3 ; Where errors are logged LOGS=tmp/ ; Our custom error handler, so we also get a pretty page for our users ;ONERROR="" ; Where the framework will look for templates and related HTML-support files UI=views/ ; Where uploads will be saved UPLOADS=assets/ ``` I'm not sure what's going wrong. Thanks In advance.

Original source