Phalconphp micro mvc路由问题

时间:2013-11-20 21:41:29

标签: phalcon

我刚开始从事简单的休息服务。 我有这样的文件夹结构:

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 ,那么它在工作中也会匹配任何网址,这也不好。

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

3 个答案:

答案 0 :(得分:3)

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

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

根据您的需要替换REQUEST_URI。

答案 1 :(得分:2)

如果您仍在寻找答案:

如果您没有.htacess,请创建一个并添加以下内容:根据tutorial

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ api/customers.php?_url=/$1 [QSA,L]
</IfModule>

然后在您的代码中,您必须编辑要匹配的路径:

$app->get('/fetchall', function() use ($app) {

为:

$app->get('/api/customers/fetchall', function() use ($app) {

这就是它的全部内容。

答案 2 :(得分:0)

您可能忘记在路线中添加/ api / customers前缀。试试这个:

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

    echo json_encode($data);
});