Skip to content

Instantly share code, notes, and snippets.

@sknuell
Created April 12, 2013 12:24
Show Gist options
  • Save sknuell/5371653 to your computer and use it in GitHub Desktop.
Save sknuell/5371653 to your computer and use it in GitHub Desktop.
Using Traits for injecting Services/Helpers
<?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