V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
skyboy
V2EX  ›  问与答

为什么 php-fastroute 是空白

  •  
  •   skyboy · 2016-12-08 08:24:49 +08:00 · 1353 次点击
    这是一个创建于 2714 天前的主题,其中的信息可能已经有所发展或是发生改变。
    为啥我 php 用 fastroute 组件,明明指定了 /的路由处理函数,但是访问后是空白。/user/222 这个也是同样的。

    <?php


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


    $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {


    $r->addRoute('GET', '/', "index" );


    $r->addRoute('GET', '/users', 'get_all_users_handler');

    // {id} must be a number (\d+)

    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');

    // The /{title} suffix is optional

    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');

    });



    // Fetch method and URI from somewhere
    $httpMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;

    $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;


    //$httpMethod = getenv['REQUEST_METHOD'];

    //$uri = getenv['REQUEST_URI'];



    // Strip query string (?foo=bar) and decode URI

    if (false !== $pos = strpos($uri, '?')) {

    $uri = substr($uri, 0, $pos);

    }

    $uri = rawurldecode($uri);



    $routeInfo = $dispatcher->dispatch($httpMethod, $uri);

    switch ($routeInfo[0]) {

    case FastRoute\Dispatcher::NOT_FOUND:

    // ... 404 Not Found

    echo "404 page not found!";

    break;

    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:

    $allowedMethods = $routeInfo[1];

    // ... 405 Method Not Allowed
    break;

    case FastRoute\Dispatcher::FOUND:

    $handler = $routeInfo[1];

    $vars = $routeInfo[2];

    // ... call $handler with $vars

    break;

    }


    function get_user_handler($id){


    echo $id;


    }


    function index(){

    echo "index";

    }

    nginx 如下:

    server {

    listen 80;

    listen [::]:80;


    server_name www.example.com example.com;


    root /fastroute/public;

    index index.php index.html;



    location / {

    try_files $uri $uri/ =404;

    # try_files $uri /index.php;
    # try_files $uri /index.php?$args;
    try_files $uri $uri/ /index.php$is_args$args;

    }

    location ~ \.php$ {

    try_files $uri $uri/ =404;

    include snippets/fastcgi-php.conf;


    # With php7.0-cgi alone:

    # fastcgi_pass 127.0.0.1:9000;

    # With php7.0-fpm:

    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

    }


    }
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3112 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:25 · PVG 08:25 · LAX 17:25 · JFK 20:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.