Php 超薄页面加载问题
因此,在修复了mustache和Apache perms之后,我现在遇到了一个问题,站点将加载索引页面,但之后的任何页面都位于(与加载失败的位置aPhp 超薄页面加载问题,php,html,apache,mustache,slim,Php,Html,Apache,Mustache,Slim,因此,在修复了mustache和Apache perms之后,我现在遇到了一个问题,站点将加载索引页面,但之后的任何页面都位于(与加载失败的位置aindex.html)/pages中 下面是Slim的index.php。我真的不明白为什么索引页面加载得很好,但是没有其他页面加载。如果你能给我指出正确的方向,我将不胜感激 示例: 最终用户myexample.com/ 位置myexample.com/pages/index.html 上面的工作很好,但如果你看下面,你应该能够理解我的问题 最终用户m
index.html
)/pages中
下面是Slim的index.php
。我真的不明白为什么索引页面加载得很好,但是没有其他页面加载。如果你能给我指出正确的方向,我将不胜感激
示例:
最终用户myexample.com/
位置myexample.com/pages/index.html
上面的工作很好,但如果你看下面,你应该能够理解我的问题
最终用户myexample.com/info
位置myexample.com/pages/info.html
最终用户是您在站点上看到的用户,但位置是文件所属的位置。
提前谢谢你,卢克
<?php
// Load up composer autoload, and instantiate the application.
require 'vendor/autoload.php';
$app = new \Slim\Slim;
// Register a singleton of the Mustache engine, and tell it to cache
$app->container->singleton('mustache', function () {
return new Mustache_Engine(array(
'cache' => 'storage',
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/pages', array('extension' => '.html'))
));
});
// Helper function to render templates
function renderTemplate($name, $data = array()) {
global $app;
if (file_exists('config.php')) {
$data = (require 'config.php');
}
$data += array(
'resourceUri' => $app->request->getResourceUri() ?: 'index',
'request' => $app->request
);
return $app->mustache->loadTemplate($name)->render($data);
}
// Loads a page by the given name/path
function loadPage($path) {
global $app;
// Set up the base path
$f = 'pages/' . $path;
if (file_exists($f . '.html')) {
// If there's an HTML file, render the mustache template
return renderTemplate($path . '.html');
} elseif (file_exists($f . '.php')) {
// If there's a PHP file, return it
return (require $f . '.php');
} elseif ($path != '404') {
// Otherwise, go get the 404 page
return loadPage('404');
} else {
// But if the user doesn't have a 404 page made, return a plain 404
$app->halt(404);
}
}
// Index page
$app->get('/', function () use ($app) {
echo loadPage('index');
});
// Route to everything else
$app->get('/.*?', function () use ($app) {
// Get the current request path
$path = $app->request->getResourceUri();
// Make sure the user isn't trying to escape and do nasty things
if (!preg_match('/^[A-z\/\-\_]+$/', $path)) {
echo loadPage('404');
}
// Send the page off to get loaded
echo loadPage($path);
});
$app->run();
我想你会发现你的问题不在于Slim,而在于你编写的自定义函数,允许Slim加载胡子模板
我强烈建议删除这些功能并使用中提供的。在这一点上,Slim的任何问题都可以更容易地诊断出来,因为不会有自定义函数需要调试
注意:虽然Slim Extras repo已被弃用,取而代之的是repo,但定制的Mustache视图(该视图尚未加入Slim Views repo)应该可以正常工作
有关参考信息,请参阅Slim文档的一节
更新:我一直在使用Slim View与我的一个新应用程序进行集成。强烈建议使用这两种方法,并且安装和配置都很简单。祝你好运 我已经尝试了很多次来解决这个问题,但是我在这里迷失了方向。有什么建议吗?