沃梦达 / 编程问答 / php问题 / 正文

在 fatfree 框架中实现命名空间

Implementing namespaces in fatfree framework(在 fatfree 框架中实现命名空间)

本文介绍了在 fatfree 框架中实现命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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]
GET /=SrcControllersIndex->index

index.php

namespace SrcControllers;

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

全局索引.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

[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 Framework 的自动加载器非常基础.它希望您定义一个或多个自动加载文件夹,每个文件夹都将映射到根命名空间.

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

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

Then a class named MyClass belonging to the FooBar namespace (full path: FooBarMyClass) 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 FooBar(自动加载器不会这样做给你).

NB: don't forget to specify namespace FooBar 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/映射到根命名空间,所以这意味着 Index 类应该属于根命名空间(完整路径: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命名空间(完整路径:ControllersIndex).

Then all the files under src/ will be autoloaded, which means the Index class should belong to the Controllers namespace (full path: ControllersIndex).

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

那么./下的所有文件都会被自动加载,也就是说Index类应该属于SrcControllers命名空间(完整路径:SrcControllersIndex).

Then all the files under ./ will be autoloaded, which means the Index class should belong to the SrcControllers namespace (full path: SrcControllersIndex).

这篇关于在 fatfree 框架中实现命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 fatfree 框架中实现命名空间

基础教程推荐