我在一个 Silex 项目中,我使用类来进行不同的处理:
$connection = new Connection($app);
$app->match('/connection', function () use ($app, $connection) {
$connexion->connectMember();
return $app->redirect($app['url_generator']->generate('goHome'));
})->method('GET|POST')->bind('doConnection');
在我的类'Connection'的函数'connectMember()'中,我有:
[...]
if($isMember){
[...]
}else{
return $this->_app['twig']->render(
'message.twig',
array('msg' => "This member does not exist.", 'class' => 'Warning'));
}
[...]
但是 render() 方法不起作用。我想显示的错误消息没有显示,而是启动了“$ app-> redirect (...)”。
如何让我的班级使用当前对象 Silex\Application ?是否有更好的方法将自定义类绑定到 Silex 应用程序的实例?
非常感谢您的回答!
版本:添加信息
如果我使用:
return $connexion->connectMember();
将显示错误消息。但这不是一个好的解决方案。'connection' 类调用也使用此代码的其他类:
$this->_app['twig']->render(...).
如何使 $ this->_app (存在于我的类中)对应于在我的控制器中创建的变量 $app?