2017-10-04 6 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