I'm on a Silex project and I use classes to different treatments:
$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');
In the function 'connectMember()' of my class 'Connection', I have :
[...]
if($isMember){
[...]
}else{
return $this->_app['twig']->render(
'message.twig',
array('msg' => "This member does not exist.", 'class' => 'Warning'));
}
[...]
But the render () method is not working. The error message I want to display is not displayed and "$ app-> redirect (...)" is launched instead.
How to make my class uses the current object Silex\Application ? Is there a better method to bind a custom class to the instance of the Silex application?
Thank you very much for your answers !
Edition : Add informations
If I use :
return $connexion->connectMember();
The error message is displayed. But it's not a good solution. The 'connection' class calls other classes that also use this code:
$this->_app['twig']->render(...).
How to make $ this->_app (present in my classes) correspond to the variable $app created in my controller?
Create a service for the
Connection
(orConnexion
??) class and inject the application:Read more about it in the documentation of silex and pimple (pimple is the container used by silex).
if you are using $app->share(...) for dependency injection you can setup something like this (it's pseudo code) :
Now doing this :
will automatically set $app when calling $app['mycontroller'] !
PS : if you don't want to use ->share() try using __invoke($app) because \Pimple::offsetGet() calls it :p