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.
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:
$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
).
$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
).
$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
).
Fat-Free is always the root namespace "\". (the following might be wrong) Since F3 loads your classes through the autoloader, you always have to add the root namespace to your own namespaces. In this case you have to change it to
namespace \Src\Controllers;
And of course you have to change it in your routes.ini too.
GET /=\Src\Controllers\Index->index
To help you finding those issues in the future you can higher the DEBUG value with
$f3->set('DEBUG', 2); // 0-3; 0=off, 3=way too much information
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