2017-05-25 59 views
1

我正在使用slim php路由器,出于某种原因,我收到此错误。未能打开流:第11行的C: xampp htdocs index.php中没有错误

警告:的file_get_contents(C:\ XAMPP \ htdocs中/视图/ users.php用户id = 3):未能打开流:在C无误差:\ XAMPP \ htdocs中\上线11

的index.php

这里是我的索引代码。

<?php 
use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 
require dirname(__FILE__) . '/vendor/autoload.php'; 
$app = new \Slim\App; 
$app->get('/users/{userId}', function ($request, $response, $args) { 
    if (!ctype_alnum($args['userId'])) { 
     $args['userId'] = '0'; 
    } 
    $body = file_get_contents(dirname(__FILE__) . "/views/users.php?userId={$args['userId']}"); 
    return $response->write($body); 
}); 
$app->run(); 
?> 

出于测试目的,我把这个在位于视图文件夹中的文件users.php

<?php 
echo $_GET['userId']; 

?> 

你可以找到https://github.com/slimphp/Slim

回答

0

这个PHP路由器试试这个,file_get_contents不能使用url参数

file_get_contents(dirname(__FILE__) . "/views/users.php"); 
+0

你有路过的用户ID的用户页面,以便它可以请求ID –

+0

时遗憾的响应晚了,我摔了个睡眠 –

+0

这将只返回显示正确的用户替代PHP源代码,它不会运行脚本。 – Barmar

0

为了执行PHP脚本你必须通过网络服务器,而不是作为本地文件访问它。所以它应该是

$body = file_get_contents(dirname($_SERVER['PHP_SELF']) . "/views/users.php?userId={$args['userId']}"); 
+0

这没有奏效dirname(__ FILE__)从你用户dirname(__ FILE__) –

+0

的文件目录开始,对于迟到的响应感到遗憾睡觉 –

+0

我知道'dirname(__ FILE __)'不起作用。它作为本地文件访问它,它不运行脚本,因为它必须通过网络服务器。 – Barmar

相关问题