2017-10-04 23 views
3

最も単純な例を使ってSlim 3とTwigでプロジェクトを作成しました。次のようにPHP組み込みサーバが静的ファイルの代わりにインデックスページを表示

フォルダ構造は次のように

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

アプリのコードは次のとおりです。

<?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(); 

、問題は(index/index.html.twig/style.cssをロードしようとすると、代わりにメインページが表示されていることです。 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

関連する問題