php - Best design method to access container functions -


i'm new slim3 , followed tutorial functions in container want access anywhere in code. here's index.php file initalise everything:

<?php use \psr\http\message\serverrequestinterface request; use \psr\http\message\responseinterface response;  // require loading vendor libraries installed composer require 'vendor/autoload.php';   $config['displayerrordetails'] = true; $config['addcontentlengthheader'] = false;  $app = new \slim\app(["settings" => $config]); $container = $app->getcontainer();  // monolog initalisation. use write: $this->logger->addinfo("what want write here"); $container['logger'] = function($c) {     $logger = new \monolog\logger('eq_logger');     $file_handler = new \monolog\handler\streamhandler("logs/app.log");     $logger->pushhandler($file_handler);     return $logger; };  // database connections $container['dbteacher'] = function ($c) {     $pdo = new pdo($_server['pgsql_connection_str']);     $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);     $pdo->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc);     return $pdo; }; $container['dbagent'] = function ($c) {     $pdo = new pdo($_server['pgsql_connection_str_agent']);     $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);     $pdo->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc);     return $pdo; };  $app->post('/{controller}/{function}', function (request $request, response $response) {     $headers = $request->getheaders();     $params = $request->getparsedbody();      $classname = $request->getattribute('controller');     $controller = new $classname($this->logger);     $function = $request->getattribute('function');      $result = $controller->$function($params);      $response->getbody()->write($result);     return $response; });  $app->run(); 

here can access logger typing $this->logger, same goes dbteacher , dbagent, can inside these containers created, when calling function different class want able access them don't want pass them in parameter because hard maintain, though having config.php class initalises these containers , $app variable , extending in every class use doesn't sound right. what's best way approach this?

you should use functions of dependency injection container (pimple) slim3 uses.

that being said, i've want of dynamically creating "controller"s isn't nice, abstraction shouldn't there , should $response->getbody()->write($result); or simpler method $response->write($result); in every controller. don't see why whole routing framework needed construct.

but anyway if wan't stay solution, can use pimple, i'll explain on example.

you have several classes different contructor parameter:

class {     public function __construct($logger) {} }  class b {     public function __construct($logger, $myhelper) {} } 

firstly add them piple container:

$container['a'] = function($c) { // or $container[a::class] type safety     return new a($c['logger']); }; $container['b'] = function($c) {     return new a($c['logger'], $c['myhelper']); }; 

and can them in route calling on container on app instance.

$app->post('/{controller}/{function}', function (request $request, response $response) {     $headers = $request->getheaders();     $params = $request->getparsedbody();      $classname = $request->getattribute('controller');     $controller = $this->getcontainer()->get($classname);     $function = $request->getattribute('function');      $result = $controller->$function($params);      $response->getbody()->write($result);     return $response; }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -