php - how to access $loader variable from within a class? -
app/config/routing.php
this how should done
$collection = new routecollection(); $collection->addcollection( // second argument type, required enable // annotation reader resource $loader->import("@appbundle/controller/", "annotation") ); return $this->collection;
i need create more complex routing system company, , need able register routes within class.
class autorouter extends appkernel { function __construct() { } public function registercontrollers() { $collection = new routecollection(); global $loader; $collection->addcollection( // second argument type, required enable // annotation reader resource $loader->import("@appbundle/controller/", "annotation") ); return $this->collection; } //(....) }
tldr; need access import function registercontrollers method, throws error
undefinedmethodexception: attempted call method "import" on class "composer\autoload\classloader"
// answer use symfony\component\routing\routecollection; class autorouter extends appkernel { private $collection; public function registercontrollers($loader) { $collection = new routecollection(); $collection->addcollection( // second argument type, required enable // annotation reader resource $loader->import("@appbundle/controller/", "annotation") ); return $this->collection; } public function getactivebundles() { return $this->registerbundles(); } } $at = new autorouter('dev', true); return $at->registercontrollers($loader);
this perfect opportunity utilize dependency injection. should inject $loader
instance type-hinted argument ensure correct object instance provided.
public function registercontrollers(loader $loader) { ...
using dependency injection make easier change concrete implementation in future without changing 'consuming' code.
happy coding!
Comments
Post a Comment