代码之家  ›  专栏  ›  技术社区  ›  Tarps

PHP内置服务器显示索引页而不是静态文件

  •  8
  • Tarps  · 技术社区  · 9 年前

    我使用Slim 3和Twig的最简单示例创建了一个项目。

    - 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

    2 回复  |  直到 9 年前
        1
  •  13
  •   Tarps    9 年前

    原因是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

        2
  •  1
  •   Eagle    6 年前

    我选中的另一个选项是在公用文件夹中运行服务器。您将不需要该脚本:

    cd public
    php -S localhost:8000
    
    推荐文章