In services Phalcon 3.0.1:
/* System module */
$di->setShared('view', function () {
$config = $this->getConfig();
$view = new View();
// ...code...
});
Not found getConfig() in zep file di.zep. where is it?
|
Sep '16 |
9 |
2180 |
0 |
The DiInterface
has no such method. Check the blog post again for the correct syntax: https://blog.phalcon.io/post/phalcon-3-0-0-released
TL;DR:
$config = new \Phalcon\Config([
'foo' => 'bar',
]);
$di->setShared('config', $config);
$di->setShared('view', function() {
$config = $this->config; // you have to access the previously set config by the magic property
// the declaration order doesnt matter, thanks to lazy loading
$view = new View();
return $view;
});
That sytnax worked in 2.0.x relase too.
What is the most appropriate syntax depends on the context.
For instance if you need to pass some arguments to the service definition (closure function arguments) at runtime, you need to use '4th approach':
$this->getDI()->get('audit', ['UPDATE Audit SET content = ?, status = ? WHERE AuditID = ?', [$this->response->getContent() ? $this->response->getContent() : 'NO RESPONSE!', $this->response->getStatusCode() ? $this->response->getStatusCode() : Constantia::DEFAULT_HTTP_STATUS, $this->auditID]]);
If you need to get shared object (i.e. already instantiated):
$this->getShared('session');
//call specific method via shared service
$jsonAsObj = $this->getShared('request')->getJsonRawBody();
etc.
I think it's better to use:
$config = $this->get('config');
It is simple and used consistently
It's not simplicity what it matters the most.
What would you do if you need to pass arguments to the service? What if you explicitly need shared service?
This is what I use for my custom services:
$this->myCustomService->request($params);
//request is a method of myCustomService service / class
So it really depends on the context / use case.
Not work: Not work: Not work:
$this->config;
Notice: Undefined property: Phalcon\Di::$config in /home/xerron/Repo/php/u-w-u.com/app/default/config/services.php on line 72
The
DiInterface
has no such method. Check the blog post again for the correct syntax: https://blog.phalcon.io/post/phalcon-3-0-0-releasedTL;DR:
$config = new \Phalcon\Config([ 'foo' => 'bar', ]); $di->setShared('config', $config); $di->setShared('view', function() { $config = $this->config; // you have to access the previously set config by the magic property // the declaration order doesnt matter, thanks to lazy loading $view = new View(); return $view; });
Edit you answer
The
DiInterface
has no such method. Check the blog post again for the correct syntax: https://blog.phalcon.io/post/phalcon-3-0-0-releasedTL;DR:
$config = new \Phalcon\Config([ 'foo' => 'bar', ]); $di->setShared('config', $config); $di->setShared('view', function() { $config = $this->config; // you have to access the previously set config by the magic property // the declaration order doesnt matter, thanks to lazy loading $view = new View(); return $view; });
This is happening because there is just no such a syntax.
Even doing $di->config
won't work. You can access it from controller like $this->config because it extends Phalcon/Di/Injectable
This is generated from latest Dev-Tools.
Service definition:
/**
* Shared configuration service
*/
$di->setShared('config', function () {
return include APP_PATH . "/config/config.php";
});
Index/bootstrap:
/**
* Include Services
*/
include APP_PATH . '/config/services.php';
/**
* Get config service for use in inline setup below
*/
$config = $di->getConfig();