Я изучаю CakePHP и следую this учебник.CakePHP контроллер пользовательская функция не распознается
При внедрении элемента управления DELETE я решил создать пользовательскую функцию, чтобы содержать некоторые дублирующие строки кода. Однако моя пользовательская функция не распознается, когда я пытаюсь ее вызвать.
Мой код выглядит следующим образом:
class PostsController extends AppController{
public $helpers = array('Html', 'Form');
public function index(){
$this->set('posts', $this->Post->find('all'));
}//index
public function view($id = null){
if(!$id){
throw new NotFoundException(__('Invalid Post'));
}
$post = $this->Post->findById($id);
if(!$post){
throw new NotFoundException(__('Invalid Post'));
}
$this->set('post', $post);
}//view
public function add(){
if($this->request->is('post')){
$this->Post->create();
if($this->Post->save($this->request->data)){
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}//add
public function edit($id=null){
idCheck($id);
$post = $this->Post->findById($id);
if(!$post)
throw new NotFoundException(__('Invalid post'));
if($this->request->is(array('post', 'put'))){
$this->Post->id = $id;
if($this->Post->save($this->request->data)){
$this->Session->setFlash(__('Your post has been updated!'));
return $this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if(!$this->request->data)
$this->request->data = $post;
}//edit
public function delete($id=null){
idCheck($id);
}
public function idCheck($id=null){
if(!$id)
throw new NotFoundException(__('Post ID required'));
if(!is_numeric($id))
throw new NotFoundException(__('Post ID must be numeric'));
}
}
Все, что я пытаюсь сделать, это назвать мою idCheck() функцию, но я получаю эту ошибку:
Error: Call to undefined function idcheck()
Вы не можете использовать его так, вы должны называть его '$ this-> idCheck ($ id);' –