【问题标题】:Implementing namespaces in fatfree framework在 fatfree 框架中实现命名空间
【发布时间】:2014-12-03 02:37:43
【问题描述】:

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

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');
    }
}

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

• 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/

我不确定出了什么问题。

提前致谢。

【问题讨论】:

  • 您遇到错误了吗?你设置autoloading了吗?
  • 我没有收到任何令人沮丧的错误,而且我被困了很长时间。

标签: php namespaces fat-free-framework


【解决方案1】:

Fat-Free Framework 的自动加载器非常基础。它希望您定义一个或多个自动加载文件夹,每个文件夹都将映射到根命名空间。

假设你定义了$f3->set('AUTOLOAD','app/;inc/'),你的文件结构是:

- app
- inc
- lib
  |- base.php
- index.php

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

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

--

所以要回答您的具体问题,具有以下文件结构:

- lib
  |- base.php
- src
  |- controllers
     |- index.php
- index.php

三种可能的配置:

配置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)。

【讨论】:

    【解决方案2】:

    Fat-Free 始终是根命名空间“\”。 (以下可能是错误的)由于 F3 通过自动加载器加载您的类,因此您始终必须将根命名空间添加到您自己的命名空间中。在这种情况下,您必须将其更改为

    namespace \Src\Controllers;
    

    当然,您也必须在您的 routes.ini 中更改它。

    GET /=\Src\Controllers\Index->index
    

    为了帮助您将来发现这些问题,您可以提高 DEBUG 值

    $f3->set('DEBUG', 2); // 0-3; 0=off, 3=way too much information
    

    【讨论】:

    • 我改变了我的命名空间,但还是一样。
    • 实际上应该是相反的,因为您的AUTOLOAD 文件夹是src/controllers/。这意味着该文件夹被视为根目录。所以你的索引在全局命名空间中:你应该删除namespace指令并调用GET /=Index->index
    • @xfra35 是的,可以,但我需要在命名空间中,因为我还需要运行测试。
    • 在这种情况下,将其保留在 Src\Controllers 命名空间中,但将您的 AUTOLOAD 更改为 ./AUTOLOAD 应始终指向根文件夹)。
    • 我创建了一个答案来详细说明可能的配置。
    【解决方案3】:

    试试这个配置 - 你的班级:

    namespace Creo\Controllers;
    

    框架路线

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

    文件夹位置

    _your_app_dir/app/Creo/Controllers
    

    您的引导文件(在本例中位于 _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;
    });
    

    希望这会有所帮助。

    【讨论】:

    • 该框架已经有一个注册的自动加载器,应该用于该任务。无需添加另一个。
    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多