在 PHP Slim 框架中,特定路由不能以 's' 结尾,否则会抛出 500 服务器错误
In PHP Slim framework a specific route can't end in 's' or it throws a 500 server error
我正在尝试使用 PHP Slim 框架创建一个获取路由。
<?php
$app->get('/bands', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
每当我调用此路由时,我都会收到 500 服务器错误并且我的 PHP 日志指出 $app
为空。但是,如果我将路线更改为:
<?php
$app->get('/band', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
一切正常。 bands 路线中的 's' 有点问题。我只是不能在乐队上放一个 's' 来让它成为乐队。
更新:None 条路线中 's' 结束!
我只是不明白为什么会这样。任何帮助将不胜感激。
如果您想知道 echoResponse(200,$response);
的作用,它只是将数组 returns 作为一个 JSON 对象发送到浏览器,HTTP 响应为 200 成功。这就是它的样子:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
这是我的 index.php:
<?php
require 'libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// The separate files that serve the routes
require_once 'bands.php';
require_once 'users.php';
require_once 'instructors.php';
require_once 'musicians.php';
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
$app->run();
然后我的路由包含在上面需要的文件中:
乐队:
<?php
$app->get('/bands', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
用户:
<?php
$app->get('/users', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
音乐家:
<?php
$app->get('/musicians', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
导师:
<?php
$app->get('/instructors', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
如果我删除尾随 's',以上所有路由都有效。
如果有帮助的话。这是我的 .htaccess:
RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Apache 是这里的罪魁祸首,这与 Slim 无关。您启用了多视图并且网络服务器正在匹配 php 个文件 (bands.php)。请求永远不会到达 Slim。
要让您的路由正常工作,请将此行添加到您的 .htaccess 文件的开头:
Options -Multiviews
有关发生的事情的进一步解释,Apache docs 的片段:
The effect of MultiViews is as follows: if the server receives a
request for /some/dir/foo, if /some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the server reads the directory
looking for files named foo.*, and effectively fakes up a type map
which names all those files, assigning them the same media types and
content-encodings it would have if the client had asked for one of
them by name. It then chooses the best match to the client's
requirements.
我正在尝试使用 PHP Slim 框架创建一个获取路由。
<?php
$app->get('/bands', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
每当我调用此路由时,我都会收到 500 服务器错误并且我的 PHP 日志指出 $app
为空。但是,如果我将路线更改为:
<?php
$app->get('/band', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
一切正常。 bands 路线中的 's' 有点问题。我只是不能在乐队上放一个 's' 来让它成为乐队。
更新:None 条路线中 's' 结束!
我只是不明白为什么会这样。任何帮助将不胜感激。
如果您想知道 echoResponse(200,$response);
的作用,它只是将数组 returns 作为一个 JSON 对象发送到浏览器,HTTP 响应为 200 成功。这就是它的样子:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
这是我的 index.php:
<?php
require 'libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// The separate files that serve the routes
require_once 'bands.php';
require_once 'users.php';
require_once 'instructors.php';
require_once 'musicians.php';
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
$app->run();
然后我的路由包含在上面需要的文件中:
乐队:
<?php
$app->get('/bands', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
用户:
<?php
$app->get('/users', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
音乐家:
<?php
$app->get('/musicians', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
导师:
<?php
$app->get('/instructors', function() {
$response["test"] = 'tester';
echoResponse(200, $response);
});
如果我删除尾随 's',以上所有路由都有效。
如果有帮助的话。这是我的 .htaccess:
RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Apache 是这里的罪魁祸首,这与 Slim 无关。您启用了多视图并且网络服务器正在匹配 php 个文件 (bands.php)。请求永远不会到达 Slim。
要让您的路由正常工作,请将此行添加到您的 .htaccess 文件的开头:
Options -Multiviews
有关发生的事情的进一步解释,Apache docs 的片段:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.