无脂肪的PHP无效路线



我目前与无脂肪框架有一个路由问题。在环顾四周并阅读文档后,我到目前为止仍无法解决它,尽管它似乎是一个简单的问题。

我使用的是从根目录中使用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.';
    }
);

当我使用上面的代码时,请转到Local -Host/最新的我介绍:

Error 404 : Not found

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

谢谢:)

您可能正在寻找框架模板引擎。

有多种构建代码的方法,但这是一个快速的基本示例:

// 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。有关设置的更多详细信息。

最新更新