0

I'm trying to use zend console and followed the documentation on their site. This is my code.

module.config.php

"router"                             => [
    "routes"                         => [
        "companies"                  => [
            "type"                   => "segment",
            "options"                => [
                "route"              => "/companies[/:action[/:id]]",
                "constraints"        => [
                    "action"         => "[a-zA-Z][a-zA-Z0-9_-]*",
                    "id"             => "[0-9]*",
                ],
                "defaults"           => [
                    "controller"     => Controller\CompaniesController::class,
                    "action"         => "index",
                ],
            ],
        ],
    ],
],
"console"                            => [
    "router"                         => [
        "routes"                     => [
            "abc1"       => [
                "options"            => [
                    "route"          => "abc1",
                    "defaults"       => [
                        "controller" => Controller\Console::class,
                        "action"     => "abc",
                    ],
                ],
            ],
        ],
    ],
],

My controller

public function abcAction() {
    $request                         =  $this->getRequest();

    if (! $request instanceof ConsoleRequest) {
        throw new RuntimeException("You can only use this action from a console!");
    }

    return "Done! abc.\n";

}

When I do php public/index.php abc1 it does nothing. shows nothing. am I missing any config?

Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31

1 Answers1

0

Example of what I've got in projects I work on:

<?php

namespace MyNameSpace;

use MyNameSpace\Console\CommandController;
use Zend\Mvc\Console\Router\Simple;

return [
    'console' => [
        'router' => [
            'routes' => [
                'name_of_command'        => [
                    'type'    => Simple::class,
                    'options' => [
                        'route'    => 'name-of-command',
                        'defaults' => [
                            'controller' => CommandController::class,
                            'action'     => 'command',
                        ],
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            // [... ] generated config
            CommandController::class => CommandControllerFactory::class,
        ],
    ],
];

Then got a Controller

<?php

namespace MyNameSpace\Console;

use Zend\Mvc\Console\Controller\AbstractConsoleController;

class CommandController extends AbstractConsoleController
{
    public function commandAction()
    {
        echo 'this is a response';
    }
}

The above works for me in without a hitch.

rkeet
  • 3,406
  • 2
  • 23
  • 49
  • It still doesn't respond to anything. I noticed that you aren't using controller factory? I'm using Zend mvc is there any different config for it? – Ali Rasheed Dec 14 '18 at 09:39
  • Added the config, but it does nothing more special than providing the Doctrine ObjectManager to the class. – rkeet Dec 14 '18 at 10:49
  • I'm afraid still not working. I tried to run `php public/index.php` command it does nothing. I mean it should show zend framework text or version but nothing. – Ali Rasheed Dec 14 '18 at 10:53
  • What do you see when you use xdebug to step through the code? Does it reach teh Factory? Can the Factory create the Controller class or does it throw a ServiceNotFoundException? Going to need more info – rkeet Dec 14 '18 at 11:02
  • I'm using sublime, could you guide me to debugger? – Ali Rasheed Dec 14 '18 at 11:03
  • Haven't used sublime in years. Please give [PhpStorm](https://www.jetbrains.com/phpstorm/) a go. For your local installation of PHP, dump the `phpinfo()` response (select all & copy, including the html/styling) [here](https://xdebug.org/wizard.php). Add suggested settings to your php.ini file and restart your php server. Xdebug should then work out of the box in PhpStorm. Additional info to configure (if needed) [here](https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html) – rkeet Dec 14 '18 at 11:06