在Fatfree框架中实现名称空间 [英] Implementing namespaces in fatfree framework
问题描述
我正在尝试在fatfree框架中使用名称空间,但由于某种原因,它无法找到下面的类是我的设置
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.ini
[routes]
GET /=Src\Controllers\Index->index
index.php
index.php
namespace Src\Controllers;
class Index {
function index($f3) {
$f3->set('name','world');
echo View::instance()->render('template.htm');
}
}
全局index.php
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();
更新:
错误:
未找到
HTTP 404(GET/)
HTTP 404 (GET /)
•index.php:13 Base-> run()
• index.php:13 Base->run()
更新2:
config.ini
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.
谢谢.
推荐答案
Fat-Free框架的自动加载器非常基础.它希望您定义一个或多个自动加载文件夹,每个文件夹都将映射到根名称空间.
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.
因此,假设您定义了$f3->set('AUTOLOAD','app/;inc/')
,文件结构为:
So let's say you define $f3->set('AUTOLOAD','app/;inc/')
and your file structure is:
- app
- inc
- lib
|- base.php
- index.php
然后将属于Foo\Bar
名称空间(完整路径:Foo\Bar\MyClass
)的名为MyClass
的类存储在app/foo/bar/myclass.php
或inc/foo/bar/myclass.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 :请不要忘记在myclass.php
的开头指定namespace Foo\Bar
(自动加载器不会为您完成此操作).
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
可能有三种配置:
$f3->set('AUTOLOAD','src/controllers/')
然后src/controllers/
下的所有文件将被自动加载,但是请记住:src/controllers/
映射到 root名称空间,因此,这意味着Index
类应该属于root名称空间(完整路径:\Index
).
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/')
然后src/
下的所有文件将被自动加载,这意味着Index
类应该属于Controllers
命名空间(完整路径:\Controllers\Index
).
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','./')
然后./
下的所有文件将被自动加载,这意味着Index
类应该属于Src\Controllers
命名空间(完整路径:\Src\Controllers\Index
).
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
).
这篇关于在Fatfree框架中实现名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!