6

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.

Ketan Mujumdar
  • 113
  • 2
  • 9
  • Are you getting an error? Have you set up [autoloading](http://fatfreeframework.com/routing-engine#TheF3Autoloader)? – Phil Dec 03 '14 at 02:45
  • I am not getting any error thats what is frustrating, and I am stuck in it for really long time . – Ketan Mujumdar Dec 03 '14 at 10:18

3 Answers3

13

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

xfra35
  • 3,653
  • 16
  • 21
1

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
sascha
  • 4,591
  • 3
  • 34
  • 51
  • I changed my namespace as you mentioned however still the same. – Ketan Mujumdar Dec 03 '14 at 09:32
  • Actually it should be the other way around since your `AUTOLOAD` folder is `src/controllers/`. That means this folder is considered as root. So your Index is in the global namespace: you should remove the `namespace` directive and call `GET /=Index->index`. – xfra35 Dec 03 '14 at 09:57
  • @xfra35 Yes that works but I need to be it in namespace as I need to run tests as well . – Ketan Mujumdar Dec 03 '14 at 10:11
  • 1
    In that case, leave it in the `Src\Controllers` namespace, but change your `AUTOLOAD` to `./` (`AUTOLOAD` should always point to the root folder). – xfra35 Dec 03 '14 at 10:47
  • I created an answer to detail the possible configurations. – xfra35 Dec 05 '14 at 09:29
0

Try this config - Your class:

namespace Creo\Controllers;

Framework routes

GET|POST / = \Creo\Controllers\IndexController->indexAction

folder location

_your_app_dir/app/Creo/Controllers

Your bootstrap file (in this case in _your_app_dir/app/)

spl_autoload_register(function ($className) {
    $filename = __DIR__ . '/' . str_replace('\\', '/', $className) . ".php";
    if (file_exists($filename)) {
        include($filename);
            if (class_exists($className)) {
            return true;
        }
    }
    return false;
});

Hope this will help.

  • 1
    that framework already has a registered autoloader, that should be used for that task. no need to add another one. – ikkez Dec 04 '14 at 12:10