2014-04-29 2 views
0

Alerter 클래스의 인스턴스를 내 컨트롤러에서 세션이있는보기로 전달하려고합니다.
이것이 가능한가요, 어떻게 구현할 수 있습니까? 순간 컨트롤러에서 개체를 세션에 전달하는 방법은 무엇입니까?

, 나는이 오류를 받고 있어요 :

내 컨트롤러는 다음과 같습니다
Symfony \ Component \ Debug \ Exception \ FatalErrorException 
main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Alerter" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition 

:

<?php 

use Acme\Repositories\Authority\Session\SessionInterface; 

class Alerter { 

    protected $type; 
    protected $message; 

    function __construct($message, $type = 'warning') 
    { 
     $this->message = $message; 
     $this->type = $type; 
    } 

    public function setMessage($message) 
    { 
     $this->message = $message; 
    } 

    public function getMessage() 
    { 
     return $this->message; 
    } 

    public function setType($type) 
    { 
     $this->type = $type; 
    } 

    public function getType() 
    { 
     return $this->type; 
    } 

} 

/** 
* Class SessionsController 
*/ 
class SessionsController extends BaseController { 

    /** 
    * @var Acme\Repositories\Authority\Session\SessionInterface 
    */ 
    protected $session; 

    /** 
    * Inject SentrySession Dependency. 
    * 
    * @param SessionInterface $session 
    */ 
    function __construct(SessionInterface $session) 
    { 
     $this->session = $session; 
    } 

    /** 
    * Logout the user. 
    * 
    * @return \Acme\Repositories\Authority\Session\Response 
    */ 
    public function logout() 
    { 
     $this->session->destroy(); 

     $alerter = new Alerter('Hello', 'success'); 

     return Redirect::to('login')->with('alert', $alerter); 
    } 

} 

그리고 내보기는 다음과 같습니다

@if (Session::has('alert')) 
    <div class="alert alert-warning"> 
     {{ Session::get('alert')->getMessage() }} 
    </div> 
@endif 

감사합니다 너의 도움으로!

+0

역 직렬화와 관련하여 몇 가지 문제가있는 것으로 보입니다. 이것은 세션으로하지 말아야 할 일로 나를 공격합니다. 세션은 직렬화가 가능해야합니다 (문자열로 저장 됨). – Halcyon

+0

어쩌면 당신은 맞다, 그러나 시도 가치가있다 :) – intelis

답변

0

Alerter 클래스는 당신이 세션에서의 인스턴스에 액세스하려고 할 때로드, 그래서 직렬화 분명히 실패하고 심지어 우리가 먼저로드해야합니다라고 우리에게 이야기되지 않습니다

Please ensure that the class definition "Alerter" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition

당신은 시도 할 수 있습니다 composer.json 수정과 autoload 섹션으로 클래스를 추가 - 바로 classmap 섹션 후이 추가 :

"psr-0": { 
    "CustomNamespace": "path/to/where/your/controller/is" 
} 

이 (실제 PA와 경로를 교체

그런 다음 컨트롤러의 파일의 상단에 해당 네임 스페이스를 추가) 컨트롤러 파일의 디렉토리에 토륨 :

namespace CustomNamespace; 

마지막으로, 다시 시도 composer dump-autoload를 실행, 지금은 제대로 작동합니다.

+0

는, 감사한다 시도 할 것이다! – intelis

관련 문제