Implementing namespaces in fatfree framework

 ̄綄美尐妖づ 提交于 2019-11-27 04:50:16

问题


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.


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/27262719/implementing-namespaces-in-fatfree-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!