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

Shared service always run in all request

Hi all,

I want to understand about the shared service In my application, I created many services as: security, view, db, logger, escaper,flash, modelCache,...

But,

In all requests, Phalcon always executes all services as above, both shared and not shared services

So, How about the performance?

For example:


$di->set('security', function() {
        var_dump(0);
        $security = new Phalcon\Security(); 
        $security->setWorkFactor(12);
        return $security;
    } , true);

Each request, always var_dump(0) is execute

Another example:

$di->set("db", function() use ($config) {
    var_dump(0);
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
         "host" => $config->host,
         "username" => $config->username,
         "password" => $config->password,
         "dbname" => $config->name
    ));
}, true);

In this examle, Each request, var_dump(0) still executes. So, Phalcon will create a connnect to DB??????

Please give for me a explanation

Thank you and have a nice day

I get different workflow on my test.

My services:

$di->setShared('router', function() {
  var_dump('Inside of "Router"');
  $router = new \Phalcon\Mvc\Router();
  $router->setDefaultNamespace('Example\\Controllers');
  $router->setDefaultController('index');
  $router->setDefaultAction('index');

  return $router;
});
$di->setShared('db', function() {
  var_dump('Inside of "DbService"');
  return new \Phalcon\Db\Adapter\Pdo\Sqlite([
    'dbname' => __DIR__ . '/db.sqlite'
  ]);
});
$di->setShared('test', function() {
  var_dump('Inside of "TestLibrary"');
  return new \Example\Library\TestLibrary();
});

My controller:

  public function indexAction() {
    echo 'Inside of #Index controller' . PHP_EOL;

    echo 'Test Library #get method: ' . get_class($this->test->get()) . PHP_EOL;
    echo 'Test Library: ' . get_class($this->test) . PHP_EOL;
  }

This is generates output:

string(18) "Inside of "Router""
Inside of #Index controller
string(23) "Inside of "TestLibrary""
string(21) "Inside of "DbService""
Test Library #get method: Example\Models\Test
Test Library: Example\Library\TestLibrary

But if i change my controller:

  public function indexAction() {
    echo 'Inside of #Index controller' . PHP_EOL;

    // echo 'Test Library #get method: ' . get_class($this->test->get()) . PHP_EOL;
    // echo 'Test Library: ' . get_class($this->test) . PHP_EOL;
  }

It will generates output:

string(18) "Inside of "Router""
Inside of #Index controller

Could you provide your code?

edited Feb '15

@Pavel Makarenko In your example:

public function indexAction() {
    echo 'Inside of #Index controller' . PHP_EOL;

    // echo 'Test Library #get method: ' . get_class($this->test->get()) . PHP_EOL;
    // echo 'Test Library: ' . get_class($this->test) . PHP_EOL;
  }

You don't call test library and DB service, so it is not be called, That's no problem and any idea here.

I mean, The shared service in phalcon is called multiple, many many times.

For example (Extend Pavel's example)

 public function indexAction() {
    echo 'Inside of #Index controller' . PHP_EOL;

    echo 'Test Library #get method: ' . get_class($this->test->get()) . PHP_EOL;
    echo 'Test Library: ' . get_class($this->test) . PHP_EOL;
  }

OUTPUT:

string(18) "Inside of "Router""
Inside of #Index controller
string(23) "Inside of "TestLibrary""
string(21) "Inside of "DbService""
Test Library #get method: Example\Models\Test
Test Library: Example\Library\TestLibrary

BUT. If in another controller:

 public function aaaAction() {
    echo 'Inside of #AAA controller' . PHP_EOL;

    echo 'Test Library #get method: ' . get_class($this->test->get()) . PHP_EOL;
    echo 'Test Library: ' . get_class($this->test) . PHP_EOL;
  }

OUTPUT:

string(18) "Inside of "Router""
Inside of #AAA controller
string(23) "Inside of "TestLibrary""
string(21) "Inside of "DbService""
Test Library #get method: Example\Models\Test
Test Library: Example\Library\TestLibrary

Test,DB, Router are shared service, but they are repeat calling.

Please explain me!!!

Thank you