Created
April 12, 2013 12:24
-
-
Save sknuell/5371653 to your computer and use it in GitHub Desktop.
Using Traits for injecting Services/Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// helper trait | |
namespace Foo\Controller\Helper; | |
trait RedirectorHelper | |
{ | |
private $_redirectorHelper; | |
public function injectRedirectorHelper(RedirectorHelper $helper) | |
{ | |
$this->_redirectorHelper = $helper; | |
} | |
} | |
// some controller | |
namespace Foo; | |
use Foo\Controller\Helper\RedirectorHelper; | |
class BarController | |
{ | |
use RedirectorHelper; | |
use SomeOtherService; | |
public function execute(Request $request, Response $response) | |
{ | |
// do sth ... | |
if ($success) { | |
$this->_redirectorHelper->redirect('/baz'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment