nay*_*fun 2 php object symfony twig silex
我正在进行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');
Run Code Online (Sandbox Code Playgroud)
在我的班级'Connection'的'connectMember()'函数中,我有:
[...]
if($isMember){
[...]
}else{
return $this->_app['twig']->render(
'message.twig',
array('msg' => "This member does not exist.", 'class' => 'Warning'));
}
[...]
Run Code Online (Sandbox Code Playgroud)
但render()方法不起作用.我不想显示我想显示的错误消息,而是启动"$ app-> redirect(...)".
如何让我的类使用当前对象Silex\Application?有没有更好的方法将自定义类绑定到Silex应用程序的实例?
非常感谢您的回答!
版本:添加信息
如果我使用:
return $connexion->connectMember();
Run Code Online (Sandbox Code Playgroud)
将显示错误消息.但这不是一个好的解决方案.'connection'类调用其他也使用此代码的类:
$this->_app['twig']->render(...).
Run Code Online (Sandbox Code Playgroud)
如何制作$ this - > _ app(出现在我的课程中)对应于我的控制器中创建的变量$ app?
为Connection
(或Connexion
??)类创建服务并注入应用程序:
use Silex\Application;
class Connection
{
private $_app;
public function __construct(Application $app)
{
$this->_app = $app;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
$app['connection'] = function () use ($app) {
return new Connection($app); // inject the app on initialization
};
$app->match('/connection', function () use ($app) {
// $app['connection'] executes the closure which creates a Connection instance (which is returned)
return $app['connection']->connectMember();
// seems useless now?
return $app->redirect($app['url_generator']->generate('goHome'));
})->method('GET|POST')->bind('doConnection');
Run Code Online (Sandbox Code Playgroud)
读取的文档中更多关于它硅石和疙瘩(疙瘩是由硅石所使用的容器).
归档时间: |
|
查看次数: |
3241 次 |
最近记录: |