2017-10-04 57 views
3

我使用最简单的示例创建了Slim 3和Twig项目。PHP内置服务器显示索引页而不是静态文件

文件夹结构如下:

- public 
    - index.php 
    - style.css 
index.php

应用程序代码如下:

<?php 
require 'vendor/autoload.php'; 

$app = new \Slim\App(); 
$container = $app->getContainer(); 

// Twig 
$container['view'] = function ($container) { 
    $view = new \Slim\Views\Twig('src/views', [ 
    'cache' => false // TODO 
    ]); 

    // Instantiate and add Slim specific extension 
    $basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/'); 
    $view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath)); 

    return $view; 
}; 

$app->get('/', function ($request, $response, $args) { 
    return $this->view->render($response, 'index/index.html.twig'); 
})->setName('index'); 

$app->run(); 

现在的问题是,试图加载/style.css显示主要的页面,而不是(index/index.html.twig) 。为什么我不能访问style.css文件?

我用它的PHP服务器内置的开发服务器,使用命令:

php -S localhost:8000 -t public public/index.php

我如何可以加载资产?这里有什么问题?

回答

3

原因是PHP内置的开发服务器是'哑'。

我必须在index.php文件中包含此检查作为第一件事。

// To help the built-in PHP dev server, check if the request was actually for 
// something which should probably be served as a static file 
if (PHP_SAPI == 'cli-server') { 
    $url = parse_url($_SERVER['REQUEST_URI']); 
    $file = __DIR__ . $url['path']; 
    if (is_file($file)) return false; 
} 

来源:https://github.com/slimphp/Slim-Skeleton/blob/master/public/index.php

相关问题