PHP上函数声明中的问题
就在我开始发现斯利姆的时候,我遇到了一个问题,我不知道如何寻找解决方案,因为它非常奇怪。 基本上,如果我声明了一个在路由调用另一个函数之后从路由调用的函数,则不会执行第一个函数 API组PHP上函数声明中的问题,php,rest,slim,Php,Rest,Slim,就在我开始发现斯利姆的时候,我遇到了一个问题,我不知道如何寻找解决方案,因为它非常奇怪。 基本上,如果我声明了一个在路由调用另一个函数之后从路由调用的函数,则不会执行第一个函数 API组 // API group $app->group('/api/:key', function () use ($app) { //print all route $app->get('/all',function () use($app){ echoRoutes()
// API group
$app->group('/api/:key', function () use ($app) {
//print all route
$app->get('/all',function () use($app){
echoRoutes();
});
// Library group
$app->group('/themer', function () use ($app) {
//get number of subscribed themer
$app->get('/count','allowed',function (){
echo "ciao";
});
//get information about the themer selected
$app->get('/:id','getThemer'); //AFTER THIS ROUTES /ciao AND /themes NOT WORK
$app->get('/ciao',function () use($app){
echoRoutes();
});
// Get book with ID
$app->get('/themes', function () use ($app) {
$articles = R::findAll('users');
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($articles));
});
//get number of submitted theme by themer
//$app->get('/:id/themes','getSubmitedThemeById');
//get information about selected theme
//$app->get('/:id/themes/:theme','getThemeById');
$app->get('/themes/:id/', function ($id) {
$articles = R::find("users","id = ?",[$id]);
echo json_encode(R::exportAll($articles));
});
});
});
带函数的外部文件
//external file with function
function getThemer($key,$id) {
$themer = R::find("themers","id = ?",[$id]);
echo json_encode(R::exportAll($themer));
return true;
}
function countThemer(){
echo "count";
$count = R::exec( 'SELECT COUNT(id) FROM themers' );
echo $count;
}
function allowed($key){
$app = \Slim\Slim::getInstance();
$params = $app->router()->getCurrentRoute()->getParams();
if(!($params["key"]=="giulio"))
$app->redirect ("http://google.com");
}
在调用getThemer并工作的路由index.php/api/giulio/themer/1之后,该路由index.php/api/giulio/themer/ciao和index.php/api/giulio/themer/themes不工作
我提前感谢你可能提供的帮助
欢迎对“一般外观”中的代码提出批评或意见更改路线顺序:
// API group
$app->group('/api/:key', function () use ($app) {
//print all route
$app->get('/all',function () use($app){
echoRoutes();
});
// Library group
$app->group('/themer', function () use ($app) {
//get number of subscribed themer
$app->get('/count','allowed',function (){
echo "ciao";
});
$app->get('/ciao',function () use($app){
echoRoutes();
});
// Get book with ID
$app->get('/themes', function () use ($app) {
$articles = R::findAll('users');
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($articles));
});
//get number of submitted theme by themer
//$app->get('/:id/themes','getSubmitedThemeById');
//get information about selected theme
//$app->get('/:id/themes/:theme','getThemeById');
$app->get('/themes/:id/', function ($id) {
$articles = R::find("users","id = ?",[$id]);
echo json_encode(R::exportAll($articles));
});
//get information about the themer selected
$app->get('/:id','getThemer');
});
});
因此,您使用route index.php/api/giulio/themer/1并希望执行3个不同的路由?index.php/api/giulio/themer是一组路由,那么内部的路由应该可以工作。我也暂时解决了这个问题,但因为顺序不好?我认为slim只需按照您给定的顺序添加所有url模式。如果你把GN/A:ID放在所有其他的前面,它将在每种情况下都匹配,每一个名字。@ LorenzoSalani,如果这个或任何答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。