【问题标题】:Fat-free php invalid route无脂php无效路由
【发布时间】:2016-06-24 17:56:44
【问题描述】:

我目前遇到了无脂肪框架的路由问题。环顾四周并阅读文档后,尽管这似乎是一个简单的问题,但我至今无法解决它。

我在根目录下使用f3的标准路由格式:

$f3->route(array('GET /', 'GET /index.php'), function($f3) {
    include 'header.html';
    include 'Home/index.html';
    include 'footer.html';
});

//search
$f3->route('GET /Browse', function() {
    include 'Browse/index.html';
});

这些路由都按预期正常工作。当我在 xammp 上输入 localhost 时,两者都返回概述的页面。两者都有一个文件结构:

-Root
-index.php
-header.html
-footer.html
--Browse
---index.html

定义新路由后,我不希望有一个包含 index.html 文件的文件夹,而是通过回显 html 来响应。

$f3->route('GET /Latest',
    function() {
        include 'submit/index.html';
        echo 'This is our home page.';
    }
);

当我使用上面的代码并转到 localhost/Latest 时,我会看到:

Error 404 : Not found

我的问题是,如何在没有后续文件夹和 index.html 文件的情况下允许直接来自 PHP 的响应。

谢谢和许多问候:)

【问题讨论】:

    标签: php fat-free-framework


    【解决方案1】:

    您可能正在寻找框架template engine

    有多种方式来构建您的代码,但这里有一个简单的基本示例:

    // index.php
    
    $f3->UI='templates/';
    
    $f3->route('GET /home',function($f3){
        $f3->main='home.html';
        $f3->title='Homepage';
        $tpl=Template::instance();
        echo $tpl->render('layout.html');
    });
    
    $f3->route('GET /contact',function($f3){
        $f3->main='contact.html';
        $f3->title='Contact us';
        $f3->address='5578 Grant Street Woodbridge, VA 22191';
        $tpl=Template::instance();
        echo $tpl->render('layout.html');
    });
    
    <!-- templates/layout.html -->
    <!DOCTYPE html>
    <title>{{ @title }}</title>
    
    <body>
      <include href="header.html"/>
      <main>
        <include href="{{ @main }}"/>
      </main>
      <include href="footer.html"/>
    </body>
    
    <!-- templates/home.html -->
    <h1>Hey hey! Welcome home!</h1>
    
    <!-- templates/contact.html -->
    <h1>Our address</h1>
    <p>{{ @address }}</p>
    

    至于 404 错误,请确保您的 .htaccess 文件配置正确,尤其是 RewriteBase 指令。如果不是这样,除/ 之外的每个 URI 都会抛出 404。有关设置的更多详细信息,请参阅here

    【讨论】:

    • 啊,非常感谢,id upvote 但在我的声誉更高之前不能在网站上。但是非常感谢,我认为这就是我一直在寻找的 :)
    猜你喜欢
    • 2014-02-06
    • 2019-12-28
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多