Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Cakephp中,如何在RESTful api的url中指定字符串?_Php_Web Services_Rest_Cakephp_Cakephp 2.1 - Fatal编程技术网

在Cakephp中,如何在RESTful api的url中指定字符串?

在Cakephp中,如何在RESTful api的url中指定字符串?,php,web-services,rest,cakephp,cakephp-2.1,Php,Web Services,Rest,Cakephp,Cakephp 2.1,根据这个Cakephp,RESTful api的简单设置如下: HTTP Method URL.method Controller action invoked GET /recipes*.method* RecipesController::index() GET /recipes/123.method RecipesController::view(123) POST /recipes*.method* RecipesController::a

根据这个Cakephp,RESTful api的简单设置如下:

HTTP Method     URL.method  Controller action invoked
GET     /recipes*.method*   RecipesController::index()
GET     /recipes/123.method     RecipesController::view(123)
POST    /recipes*.method*   RecipesController::add()
PUT     /recipes/123*.method*   RecipesController::edit(123)
DELETE  /recipes/123.method     RecipesController::delete(123)
POST    /recipes/123*.method*   RecipesController::edit(123)
这里所有的URL参数都是数字的,即123。 当我试着用绳子的时候

GET     /recipes/test.json  RecipesController::view(123)
这给了我一个错误:

{
   code: "404"
   url: "/myproject/recipes/test.json"
   name: "Action RecipesController::test() could not be found."
}
这里是URL

     "/myproject/recipes/test.json" // doesn't work

but 
      "/myproject/recipes/123.json" // works 
我使用了默认的
Router::mapResources('recipes')

提前谢谢

那么,读取该段代码时,在点之前传递的值会自动匹配到
id
UUID
。就在那个API中有参数定义

“id”-匹配id时要使用的正则表达式片段。通过 默认情况下,匹配整数值和UUID

mapResources
所做的只是简单地添加许多带有一些预先建立的选项的
Router::connect
(其基本形式为:controller/:action/:id

因此,如果规则是将ID(cake认为是int)与正则表达式相匹配,那么显然您的字符串没有通过验证。因此,
路由器
跳过该
连接
规则并转到另一个,直到其中一个匹配为止。匹配的是:controller/:action.extension(可能)的形式。这就是为什么会出现这样的错误,
test
显然不是一个动作

幸运的是,
mapResources
为定制提供的选项之一是匹配
$id
的规则

要将字符串选项添加为“id”(因为这是REST操作在使用
mapResources
添加连接路由时将接收的唯一变量),请更改验证该规则的正则表达式,如下所示

Router::mapResources('recipes', array('id'=>'[0-9A-Za-z]'));
或者任何你想制定的规则(我不喜欢正则表达式,所以试着根据你的需要调整它)

看一看API,看看您还可以添加哪些其他选项


记住 MultReals是为了让你的生活更轻松,所以如果你需要更多的复杂路线或者更多的PARAMS或者额外的东西,考虑忘记<代码> MaPrase并自己构建路由(就像你所提供的链接的页面底部所说)。.

在您的路线中定义以下代码:

// used for the rest API 
$routes->extensions(['json','xml']); // type of format you want to get response
$routes->resources('Api');
然后在controller文件夹中为API创建一个控制器,如下所示

<?php
namespace App\Controller;
use Cake\I18n\Time;
use Cake\Database\Type; 
Type::build('date')->setLocaleFormat('yyyy-MM-dd'); // customize date format

// src/Controller/RecipesController.php
class ApiController extends AppController
{

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
        // load Model 
        $this->loadModel('Sales'); // load model to fetch data from database
        $this->Auth->allow();      // allow URL to public in case of Auth check 
    }

    public function beforeFilter(\Cake\Event\Event $event)
    {
        parent::beforeFilter($event);
        $this->loadComponent('RequestHandler');     
        $this->loadComponent('Flash');

    }

    public function index($fromdate = null, $todate = null)
    {
        //set date range to fetch the sales in particular date 
        if(!empty($_GET['fromdate_utc']) && !empty($_GET['todate_utc'])){
            // if from amd to date are same add +1 day in to date to get result 
            $to_date = date('Y-m-d', strtotime($_GET['todate_utc'] . ' +1 day'));
            $dateRage = array('Sales.SalesDate >= ' => $_GET['fromdate_utc'], 'Sales.SalesDate <=' => $to_date);
        }else{
            $dateRage = array();                    
        }

        $conditions = array(
            'and' => array($dateRage),
        );

        //$this->Auth->allow();
        $sales= $this->Sales->find('all', array(
                        'conditions' => $conditions
                    ))
                    ->select(['SalesNo', 'SalesDate', 'TotalValue', 'TotalDiscount', 'NetTotal', 'PaymentMode', 'Status'])
                    ->where(['StoreId' => '1']);
       // set data for view or response of API
        $this->set([
            'sales' => $sales,
            '_serialize' => ['sales']
        ]);
    }

}

?>
如何在API URL中传递参数以进行以下JSON格式检查:-

https://example.com/api/index.xml?fromdate_utc=2016-10-03&todate_utc=2016-10-03
https://example.com/api/index.json?fromdate_utc=2016-10-03&todate_utc=2016-10-03

你试过改变正则表达式吗(正如我说的,我对它们不好)。尝试使用
[0-9a-zA-Z]+
(我现在意识到我只允许一个字符…告诉你我不擅长)太好了:)记住,如果你想允许空格或连字符之类的字符,你必须调整正则表达式。