CakePHP将HTTP请求映射到正确的操作 [英] CakePHP mapping HTTP request to the correct action

查看:57
本文介绍了CakePHP将HTTP请求映射到正确的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的应用程序中设置RESTful功能,无论遇到什么操作,我都会遇到一个问题,即它是指向特定控制器 index 操作的路由器。我一直在尝试调用 add view 动作,但是它们的路由不正确。这是尝试调用视图操作时遇到的问题:

  { code:404, url: \ / application\ / rest_customers\ / 54.json,名称:找不到操作RestCustomersController :: 54()。} 

这是全部设置的方式。 RestCustomersController

 类RestCustomersController扩展了AppController {

public $ uses = array('Customer');
public $ helpers = array('Html','Form');
public $ components = array('RequestHandler');

公共功能index(){
}

公共功能视图($ id = null){

$ customer = $ this -> Customer-> find('first',array(
'conditions'=> array('Customer.id'=> $ id))));

$ this-> set(array(
'customer'=> $ customer,
'_serialize'=> array('customer')
)); }}

以下是路线:

  Router :: mapResources('customers'); 
Router :: parseExtensions('json','xml','csv','pdf');

这里是 AppControllers beforeFitler函数:

  if(in_array($ this-> params ['controller'],array('rest_customers'))){
$ this-> Auth-> allow();
$ this->安全性-> unlockedActions =数组(添加,索引,视图);
} else {
$ this-> Security-> unlockedActions = array('add','edit');
$ this-> Auth-> allow('index','view');
$ this-> set('logged_in',$ this-> Auth-> loggedIn());
$ this-> set('current_user',$ this-> Auth-> user());
}}

非常感谢任何帮助。

解决方案

您正在为名为 Customers 的控制器创建REST路由,但实际上正在访问名为 RestCustomers ,因此您遇到的是预期的行为,因为 RestCustomers 控制器根本没有连接REST路由。 / p>

您应该将控制器重命名为 CustomersController ,或将映射更改为使用正确的名称,以便它可以连接到 RestCustomersController

的路由

  Router :: mapResources('rest_customers') ; 


I have been trying to set up RESTful functionality in my app and I face a problem of whatever action I call it is router to that particular controllers index action. I have been trying to call add and view actions, but they are just not being routed properly. Here is the resposne I get when trying to call the view action:

{"code":404,"url":"\/application\/rest_customers\/54.json","name":"Action RestCustomersController::54() could not be found."}

And here is the way it is all set up in. RestCustomersController:

class RestCustomersController extends AppController {

    public $uses = array('Customer');
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler');

    public function index() {
    }

    public function view($id=null){

    $customer = $this->Customer->find('first', array(
          'conditions'=>array('Customer.id'=> $id)));

        $this->set(array(
            'customer' => $customer,
            '_serialize' => array('customer')
        )); }}

Here are the routes:

Router::mapResources('customers');
Router::parseExtensions('json', 'xml', 'csv', 'pdf');

And here os the AppControllers beforeFitler function:

if(in_array($this->params['controller'], array('rest_customers'))){
    $this->Auth->allow();
    $this->Security->unlockedActions = array('add','index', 'view');
}else{
    $this->Security->unlockedActions = array('add', 'edit');
    $this->Auth->allow('index', 'view');
    $this->set('logged_in', $this->Auth->loggedIn());
    $this->set('current_user', $this->Auth->user());
}}

Any help is very much appreciated.

解决方案

You are creating REST routes for a controller named Customers, but you are actually accessing a controller named RestCustomers, so what you are experiencing is the expected behavior as there simply are no REST routes connected for the RestCustomers controller.

You should either rename your controller to CustomersController, or change your mapping to use the correct name so that it will connect the routes to RestCustomersController

Router::mapResources('rest_customers');

这篇关于CakePHP将HTTP请求映射到正确的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆