Error: Unable to access CakePHP directly

The element file that I call:

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

The action file I call:

  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']; } 

Getting the error below .. !!

 Private Method in BrandsController Error: BrandsController::buyer_getnames() cannot be accessed directly. 

Please, help

+4
source share
2 answers

Request action follows normal URL routing rules

If you use prefix routing , you cannot access function prefix_foo() through the URL of the form /controller/prefix_foo - it needs to be the corresponding url prefix: /prefix/controller/foo .

So your request action request should be:

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

Please note: if the only thing this method does is call the model method, you better just do:

 $model = ClassRegistry::init('Brand'); $brand = $model->someMethod(); 
+5
source

You can allow unauthorized access to an action if your action is requested using the requestAction method.

For instance:

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

This may also work (not verified):

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

let me know if I can help you more.

+2
source

Source: https://habr.com/ru/post/1488509/


All Articles