4

我正在调用的元素文件:

      $brand = $this->requestAction('brands/buyer_getnames/');

我正在调用的操作文件:

    public function buyer_getnames(){
               $newid=$this->Auth->User('brand_id');
                   $name=$this->Brand->find('first',array('conditions'=>array('Brand.id'=>$newid),'recursive'=>-1));
    return $name['Brand']['name'];
}

下面出现错误..!!

Private Method in BrandsController

Error: BrandsController::buyer_getnames() cannot be accessed directly.

请帮忙

4

2 回答 2

5

请求操作遵守正常的 url 路由规则

如果您使用前缀路由,则无法function prefix_foo()通过表单的 url访问/controller/prefix_foo- 它必须是相应的前缀 url: /prefix/controller/foo

因此,您的请求操作调用应该是:

$brand = $this->requestAction('/prefix/brands/getnames/');

请注意,如果该方法所做的唯一事情是调用模型方法,那么您最好简单地执行以下操作:

$model = ClassRegistry::init('Brand');
$brand = $model->someMethod();
于 2013-06-28T08:23:28.563 回答
2

如果您的操作是使用 requestAction 方法请求的,您可以允许未经授权的访问操作。

例如:

public function beforeFilter() {
    parent::beforeFilter();

    if ($this->request->is('requested') && $this->request->params['action'] == 'index') {
        $this->Auth->allow(array('index'));
    }
}

这也可能有效(尚未测试):

public function index() {
    if ($this->request->is('requested')) {
        $this->Auth->allow(array('index'));
    }
}

让我知道我是否可以为您提供更多帮助。

于 2013-06-27T15:25:02.157 回答