【发布时间】:2013-11-20 21:41:28
【问题描述】:
我刚刚开始研究一个简单的 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,这是不好的。
提前感谢您的帮助。 谢谢 亚当
【问题讨论】:
标签: phalcon