为什么 php-fastroute 是空白

2016-12-08 08:24:49 +08:00
 skyboy
为啥我 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;

}


}
1490 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/326078

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX