[英]Slim Page loading Issues
因此,在修復了胡子和Apache燙發之后,我現在遇到了一個問題,該站點將加載索引頁面,但是此頁面之后的任何頁面都位於(無法加載)/ pages中(位於index.html
的同一位置)。
您可以在下面看到的是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加載Mustache模板的自定義函數。
我強烈建議刪除這些功能,並使用Slim-Extras repo的views目錄中提供的自定義Slim Mustache視圖 。 到那時,可以輕松診斷出Slim的任何問題,因為不會調試任何自定義功能。
注意 :雖然不建議使用Slim-Extras存儲庫,而推薦使用Slim-Views存儲庫,但是自定義的Moustache視圖(尚未加入Slim-Views存儲庫) 應該可以正常工作。
作為參考,請參閱Slim文檔的“ 自定義視圖”部分。
更新 :我一直在使用Dearon的Slim Mustache庫與我的新應用程序中的Slim View集成以及Justin Hileman的Mustache的PHP實現 。 強烈推薦兩者,並且易於安裝和配置。 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.