2013-05-27 2 views
1

내 세션에 어떤 문제가 있습니까? 왜 그것이 대상이 아닌가? 나는 cakePHP quick start blog example을 시도하고있다. 나는 모든 코드를 복사 한,하지만 난 편집 할 때 나타나는 삭제 또는 블로그 메시지를 추가하는이 오류를 유지할 수 없습니다 : 멤버 함수의 setFlash (에cakePHP 세션 비 목적 블로그 예

전화)이 아닌 객체를

세션 변수를보기 위해 뷰에 디버그 라인을 삽입 했으므로 세션 데이터가 정상적으로 보입니다. 또한 컨트롤러에 '세션'도우미를 추가하여 도움이되는지 확인했습니다.

컨트롤러 :

class PostsController extends AppController { 

public $helpers = array('Html', 'Form','Session'); 

public function index() { 
    $this->set('posts', $this->Post->find('all')); 
} 

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); 
} 

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.'); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash('Unable to add your post.'); 
     } 
    } 
} 
public function edit($id = null) { 
    if (!$id) { 
    throw new NotFoundException(__('Invalid post')); 
    } 
    $post = $this->Post->findById($id); 
    if (!$post) { 
    throw new NotFoundException(__('Invalid post')); 
    } 
    if ($this->request->is('post') || $this->request->is('put')) { 
    $this->Post->id = $id; 
    if ($this->Post->save($this->request->data)) { 
     $this->Session->setFlash('Your post has been updated.'); 
     $this->redirect(array('action' => 'index')); 
    } else { 
     $this->Session->setFlash('Unable to update your post.'); 
    } 
    } 
    if (!$this->request->data) { 
    $this->request->data = $post; 
    } 
} 

public function delete($id) { 
    if ($this->request->is('get')) { 
    throw new MethodNotAllowedException(); 
    } 
    if ($this->Post->delete($id)) { 
    $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.'); 
    $this->redirect(array('action' => 'index')); 
    } 
} 
} 
?> 

나는 도우미에 '세션'추가했고 난 그냥 $ this-> 세션 -> 플래시()를 사용하지만 세션 개체 수 없을 것 같다 시도했습니다. 세션이 올바르게 시작되며, 세션 데이터는 존재한다 :

배열 ( [구성] => 배열 ( [의 userAgent] => fd7f6d79160a3f20a706f3fed20eff02 [시간] => 1369643237 [카운트] => 10 )

사용 가능한 세션 인스턴스가없는 이유를 모르겠습니다.

답변

3

세션 구성 요소가 누락되었습니다.

당신은 당신의 PostsController 내부를 추가해야합니다

class PostsController extends AppController { 
    public $components = array('Session'); 
    //... 
} 
+0

감사합니다! 그들은 그 라인을 예제에 집어 넣었지만 정말로 언급하지 않았고 나는 그것을 알아 차리지 못했습니다. – user6972

+1

예제에서는 [how to] (http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses)와 연결되어 있지 않지만 명확하게 언급합니다. 'SessionComponent - 그리고 SessionHelper - 당신이 그것을 사용할 어떤 컨트롤러에서. 필요한 경우 AppController에 포함하십시오. ' –