gpt4 book ai didi

Phalconphp micro mvc路由问题

转载 作者:行者123 更新时间:2023-12-02 21:42:36 27 4
gpt4 key购买 nike

我刚刚开始开发一个简单的 Restful 服务。我的文件夹结构如下:

root
- /api
--/api/customers.php

例如,在浏览器中我打算调用http://domain/api/customers/fetchall

但是,我只调用了 notFound 处理程序。 customers.php 中的代码是:

<?php

use Phalcon\DI\FactoryDefault,
Phalcon\Mvc\Micro,
Phalcon\Config\Adapter\Ini;

$di = new FactoryDefault();
$di->set('config', function() {
return new Ini("config.ini");
});

$app = new Micro($di);

/**
* Create new customer
*/
$app->post('/create', function(){});

/**
* Retrieve all customers
*/
$app->get('/fetchall', function() use ($app) {
$data = array();
$data[] = array(
'id' => '123456',
'name' => 'customerName',
);

echo json_encode($data);
});

/**
* Find customer by name
*/
$app->get('/search/{name}', function($name){});

/**
* Find customer by email
*/
$app->get('/search/{email}', function($email){});

/**
* Find customer by postcode
*/
$app->get('/search/{postcode}', function($postcode){});

/**
* Move a customer
*/
$app->put('/move/{oldpostcode}/{newpostcode}', function($oldpostcode, $newpostcode){});

/**
* Delete customer
*/
$app->delete('/delete/{id:[0-9]+}', function($id) use ($app) {
$response = new Phalcon\Http\Response();
$response->setJsonContent(array('status' => 'OK'));
return $response;
});

$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'This is crazy, but this page was not found!';
});

//echo $_SERVER['REQUEST_URI'];
$app->handle();

如果我使用 / 而不是 /fetchall 那么可以工作,但它也匹配任何网址,这不好。

提前感谢您的帮助。谢谢亚当

最佳答案

看起来像一个错误,但您需要指定您希望应用程序处理的 URL:

$app->handle(filter_input(INPUT_SERVER, 'REQUEST_URI'));

将 REQUEST_URI 替换为您需要的内容。

关于Phalconphp micro mvc路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com