在fatfree框架中实现名称空间

Ket*_*dar 6 php namespaces fat-free-framework

我试图在fatfree框架中使用命名空间,但不知何故它无法找到以下类是我的设置

routes.ini

[routes]
GET /=Src\Controllers\Index->index
Run Code Online (Sandbox Code Playgroud)

的index.php

namespace Src\Controllers;

class Index {
    function index($f3) {
        $f3->set('name','world');
        echo View::instance()->render('template.htm');
    }
}
Run Code Online (Sandbox Code Playgroud)

全球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();
Run Code Online (Sandbox Code Playgroud)

更新:

错误:

未找到

HTTP 404(GET /)

•index.php:13 Base-> run()

更新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/
Run Code Online (Sandbox Code Playgroud)

我不确定出了什么问题.

提前致谢.

xfr*_*a35 11

无脂框架的自动加载器非常基础.它希望您定义一个或多个自动加载文件夹,每个文件夹都将映射到根命名空间.

所以我们假设您定义$f3->set('AUTOLOAD','app/;inc/')并且您的文件结构是:

- app
- inc
- lib
  |- base.php
- index.php
Run Code Online (Sandbox Code Playgroud)

然后,一个名为MyClass属于Foo\Bar命名空间的类(完整路径:) Foo\Bar\MyClass应该存储在app/foo/bar/myclass.php或者inc/foo/bar/myclass.php(记住:我们指定了两个自动加载文件夹).

注意:不要忘记namespace Foo\Bar在开头指定myclass.php(自动加载器不会为你做).

-

因此,要回答您的具体问题,请使用以下文件结构:

- lib
  |- base.php
- src
  |- controllers
     |- index.php
- index.php
Run Code Online (Sandbox Code Playgroud)

三种配置是可能的:

配置1

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

然后,所有文件src/controllers/都将被自动加载,但请记住:src/controllers/映射到根命名空间,这意味着Index该类应该属于根命名空间(完整路径:) \Index.

配置2

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

然后,所有文件src/都将被自动加载,这意味着Index该类应该属于Controllers命名空间(完整路径:) \Controllers\Index.

配置3

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

然后,所有文件./都将被自动加载,这意味着Index该类应该属于Src\Controllers命名空间(完整路径:) \Src\Controllers\Index.