2013-11-20 28 views
1

我剛開始致力於一個簡單的寧靜服務。 的文件夾結構我已經是像這樣:Phalconphp micro mvc路由問題

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以及這也是白搭。

提前感謝幫助。 感謝 亞當

回答

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

看起來像一個bug,但你需要指定您希望您的應用程序處理的網址:

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

由你所需要的替換REQUEST_URI。

+0

用這個在Micro v 2.0.8中獲得正確的URI – Emi

0

你可能忘了在路由中添加/ api/customers前綴。試試這個:

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

    echo json_encode($data); 
});