We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Where do is getConfig()?

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?



77.7k
Accepted
answer
edited Sep '16

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;
});
edited Sep '16

3 Ways to get services in $di: phalcon 3.0.1

$di->setShared('view', function () {
    // 1
    $config = $this->getConfig();
    // 2
    $config = $this->get('config');
    // 3
    $config = $this->getDI()->get('config');
    // ...code...
});

What is the most appropriate syntax? The most consistent?

edited Sep '16

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.

edited Sep '16

I think it's better to use:

$config = $this->get('config'); 

It is simple and used consistently

edited Sep '16

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-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;
});

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-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;
});

You sure you have 3.0.0 phalcon ?

Yes,

$di->setShared('view', function() {
    $config = $this->config; // This return Notice: Undefined property: Phalcon\Di::$config
    $view = new View();
    return $view;
});
edited Sep '16

But you have config added to di, right ?

Ajam.... YES. :3

services.php

$di->setShared('config', function() {
    /// code...   
    return $config;
});

$di->setShared('view', function() {
    $config = $this->config; // This return Notice: Undefined property: Phalcon\Di::$config
    //...code...
    $view = new View();
    return $view;
});

This is Bug?

edited Sep '16

What OS and php handler(apache2 module or php-fpm ?)

Well actually you need to use $this->get("config")

Lubuntu 16.04 nginx version: nginx/1.10.0 (Ubuntu) PHP 7.0.8-0ubuntu0.16.04.2 (fpm-fcgi)

edited Sep '16
 Notice: Undefined property: Phalcon\Di::$config
                                         ^
                                         |
                            $ <- this is correct

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

edited Sep '16

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();