我想将一个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 :(得分:0)
当您尝试从会话中访问它的实例时,未加载Alerter
类,因此反序列化显然会失败,甚至告诉我们应该首先加载它:
请确保在 unserialize()被调用之前加载了您正在尝试操作的对象的类定义“Alerter”,或者提供__autoload()函数来加载类定义
您可以尝试修改composer.json
并将您的课程添加到autoload
部分 - 在classmap
部分之后添加:
"psr-0": {
"CustomNamespace": "path/to/where/your/controller/is"
}
(将路径替换为控制器文件目录的实际路径)
然后在控制器文件的顶部添加该命名空间:
namespace CustomNamespace;
最后,运行composer dump-autoload
再试一次,它现在应该正常工作。