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

Namespaces vs app in client mode

I started using namespaces according to: https://olddocs.phalcon.io/en/3.0.0/reference/namespaces.html Next I wrote my own command according to: https://docs.phalcon.io/de/3.2/application-cli

But:

  • I had to remove namespaces from my Task classes
  • And like above, that part of code:

    $di->set(
    "dispatcher",
    function () {
        $dispatcher = new \Phalcon\Mvc\Dispatcher();
    
        $dispatcher->setDefaultNamespace(
            "App\\Controller"
        );
    
        return $dispatcher;
    }
    );

    Bacause I have an error "Call to undefined method ?::settaskname()

Is a good practise to use namespaces in Phalcon? It looks like makeshift workaround what makes a lot of troubles in future.

edited Jan '18

Namespaces are a must in larger projects, the pattern allows for logical grouping of classes. Phalcon itself is agnostic about namespaces, it depends on your setup.

You have to use the proper classes for the console environment, like Phalcon\Cli\Dispatcher instead of Phalcon\Mvc\Dispatcher and Phalcon\Di\FactoryDefault\Cli instead of Phalcon\Di\FactoryDefault



8.3k

Does it make a sense create a larger project in micro-fw like Phalcon? I've just added in my services.php

        if (php_sapi_name() == 'cli') {
            $dispatcher = new \Phalcon\Cli\Dispatcher();
        } else {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setDefaultNamespace("App\\Controller");
        }

Phalcon\Di\FactoryDefault is used by default (according to doc) Now namespaces work with exception of tasks classes.

Phalcon\Di\FactoryDefault is used by default (according to doc)

Then the docs may be off, Cli apps should use Phalcon\Di\FactoryDefault\Cli, because that DI automatically populates the correct services for the environment.

https://github.com/phalcon/cphalcon/blob/master/phalcon/di/factorydefault.zep#L39

https://github.com/phalcon/cphalcon/blob/master/phalcon/di/factorydefault/cli.zep#L43



8.3k

I have:

use Phalcon\Di\FactoryDefault\Cli as CliDI;

// Using the CLI factory default services container
$di = new CliDI();

// Create a console application
$console = new ConsoleApp();

$console->setDI($di);

And it looks OK to me, but it doesn't solve problem with task classes and also I need services are provided by included file services.php, common for client and server mode (like config). At present my solutions are:

  • conditions in services.php
  • lack of namespaces in task classes
// services-cli.php
// Declaration of CLI specific services
global $di;

$di = new Phalcon\Di\FactoryDefault\Cli();

$di->setShared('dispatcher', function() {
    $dispatcher = new Phalcon\Cli\Dispatcher();
    $dispatcher->setDefaultNamespace("App\\Controller");
    return $dispatcher;
});

include __DIR__ . '/services.php';
// services-web.php
// Declaration of WEB specific services
global $di;

$di = new Phalcon\Di\FactoryDefault();
$di->setShared('flash', function() {
    return new Phalcon\Flash\Direct([
        'error' => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info',
        'warning' => 'alert alert-warning'
    ]);
});

include __DIR__ . '/services.php';
// services.php
// Declaration of shared services between CLI and WEB
global $di;

$di->setShared('db', function() {
    return new Mysql($config);
});

$di->setShared('myCommonService', function() {
    // yada-yada
    return 'foo';
});

All you have to do is require either the services-cli.php or service-web.php depending on your environment.