zfcampus / zf-apigility

BSD 3-Clause "New" or "Revised" License
258 stars 52 forks source link

Unable to use console routing #205

Closed Vinnythetrue closed 6 years ago

Vinnythetrue commented 6 years ago

I'm working on my first Apigility application, so far so good. It works perfectly with http to serve as an API for a web frontend application. Now, I'm trying to implement scheduled cron operations through console routing. But no matter what I tried, I can't get it to work...

At first, I simply added this in my API module configuration :

'console' => [
    'router' => [
        'routes' => [
            'test_route' => [
                'options' => [
                    'route'    => '/test',
                    'defaults' => [
                        'controller' => 'mymodule\V1\Console\Test\Controller',
                        'action' => 'test'
                    ],
                ],
            ],
        ],
    ],
],

Then trying to call with :

php ./public/index.php /test The result was "No base path provided". So I added this in my module configuration :

'view_manager' => [
       'base_path' => __DIR__ . '/../',
]

Now, no matter what I try, I always get a 404 "The requested URL could not be matched by routing.". I found examples over the internet running commands like "zf foo bar" to run a zend application as command line, but I don't have that command on my (Windows 8.1) system. Should I install this ? I'm using composer, and I tried to install various packages like zfcampus/zf-console or zendframework/zend-console, but without any noticeable changes...

I'm new with Apigility but with Zend as well... So my main issue I suppose is that I have no idea if my problem comes from one or the other...

weierophinney commented 6 years ago

When you route at the console level, you are defining rules for parsing console arguments, options, and flags — not URI paths. Please read the zend-mvc-console documentation, as well as the zend-console route matching documentation to understand how to create routes for the console.

That said: the MVC <-> console integration is deprecated. We recommend using symfony/console instead. You can still pull services from your application container when creating your command instances.

Vinnythetrue commented 6 years ago

thanks for your time, but I cannot see ho it answers my question... I tried with various parameters, and the only thing in my example that could seem like an url is the leading "/" of my route. But I tried other stuff, like copy-paste examples from Zend documentations found here and there (some times containing syntax errors). I tried so many things with different small adjustments, I cannot believe there is not something else... And if I ear you well about that deprecation stuff, I would need to pull out a completely new framework only in order to have such simple feature that is the console ?? Seems quite overkill :(

froschdesign commented 6 years ago

@Vinnythetrue The answer is quite simple and was already answered by @weierophinney: your route is wrong. You are on the console and there is no slash or other elements of a URI. Compare with the second code example: https://docs.zendframework.com/zend-mvc-console/routing/#routing

Vinnythetrue commented 6 years ago

and furthermore, what about the "base_path" stuff ? To be noted that with any base_path defined in the config, the http calls no longer work as the css and js files get referenced badly, and this apparently no matter what value I put in it (in fact, the sub path part in these css and js links don't seem right as they are like a/b/c although my directories on the HDD are like a/public/b/c...

Vinnythetrue commented 6 years ago

sorry @froschdesign I didn't see your comment earlier...

In fact I tried with other stuff, including examples provided in the documentation, and without any slashes anywhere.

I'm trying another example :

module.config.php

    'console' =>[
        'router' => [
            'routes' => [
                'console.foo' => [
                    'options' => [
                        'route' => 'delete user <userEmail>',
                        'defaults' => [
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'password'
                        ],
                    ],
                ],
            ],
        ],
    ],

then I call :

php ./public/index.php delete user ti@ad.com

I still get exactly the same "URL could not be matched by routing" error....

Right now I'm trying to deploy the app on a linux server, I think maybe its related to my windows machine ?

Vinnythetrue commented 6 years ago

I also tried these routes :

'console' =>[
        'router' => [
            'routes' => [
                'console.foo' => [
                    'options' => [
                        'route' => 'delete user <userEmail>',
                        'defaults' => [
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'password'
                        ],
                    ],
                ],
                'console.foo2' => [
                    'options' => [
                        'route' => 'test',
                        'defaults' => [
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'password'
                        ],
                    ],
                ],
                'console.foo3' => [
                    'options' => [
                        'defaults' => [
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'password'
                        ],
                    ],
                ],
                'console.foo4' => [
                    'route' => 'test',
                ],
                'console.foo5' => [
                ],
                'console.foo6' => [
                    'type' => 'catchAll'
                ],
            ],
        ],
    ],

And I tried of course different associated command, with parameters, without...

php ./public/index.php test
php ./public/index.php
php ./public/index.php delete user test@dom.aine

No matter what I do, I always get the same "URL not matching any route" response.... And apparently not related to my machine, same on my linux server

Just to emphasis one thing, I really get a 404 displayed in the console (the HTML code response being displayed). Maybe it's not detecting it runs in console ??

weierophinney commented 6 years ago

@Vinnythetrue Can you run composer show and paste the output in a comment, please? I have a suspicion as to the root cause, but that will help me verify.

Vinnythetrue commented 6 years ago

@weierophinney below you'll find the asked output :

bshaffer/oauth2-server-php             v1.10.0 OAuth2 Server for PHP
container-interop/container-interop    1.2.0   Promoting the interoperab...
doctrine/instantiator                  1.1.0   A small, lightweight util...
herrera-io/json                        1.0.3   A library for simplifying...
herrera-io/phar-update                 1.0.3   A library for self-updati...
justinrainbow/json-schema              1.6.1   A library to validate a j...
kherge/version                         1.0.1   A parsing and comparison ...
michelf/php-markdown                   1.8.0   PHP Markdown
myclabs/deep-copy                      1.8.1   Create deep copies (clone...
paragonie/random_compat                v2.0.17 PHP 5.x polyfill for rand...
phar-io/manifest                       1.0.3   Component for reading pha...
phar-io/version                        2.0.1   Library for handling vers...
phpdocumentor/reflection-common        1.0.1   Common reflection classes...
phpdocumentor/reflection-docblock      4.3.0   With this component, a li...
phpdocumentor/type-resolver            0.4.0
phpspec/prophecy                       1.8.0   Highly opinionated mockin...
phpunit/php-code-coverage              6.1.4   Library that provides col...
phpunit/php-file-iterator              2.0.2   FilterIterator implementa...
phpunit/php-text-template              1.2.1   Simple template engine.
phpunit/php-timer                      2.0.0   Utility class for timing
phpunit/php-token-stream               3.0.1   Wrapper around PHP's toke...
phpunit/phpunit                        7.4.3   The PHP Unit Testing fram...
psr/container                          1.0.0   Common Container Interfac...
psr/link                               1.0.0   Common interfaces for HTT...
sebastian/code-unit-reverse-lookup     1.0.1   Looks up which function o...
sebastian/comparator                   3.0.2   Provides the functionalit...
sebastian/diff                         3.0.1   Diff implementation
sebastian/environment                  3.1.0   Provides functionality to...
sebastian/exporter                     3.1.0   Provides the functionalit...
sebastian/global-state                 2.0.0   Snapshotting of global state
sebastian/object-enumerator            3.0.3   Traverses array structure...
sebastian/object-reflector             1.1.1   Allows reflection of obje...
sebastian/recursion-context            3.0.0   Provides functionality to...
sebastian/resource-operations          2.0.1   Provides a list of PHP bu...
sebastian/version                      2.0.1   Library that helps with m...
seld/jsonlint                          1.7.1   JSON Linter
symfony/polyfill-mbstring              v1.10.0 Symfony polyfill for the ...
theseer/tokenizer                      1.1.0   A small library for conve...
true/punycode                          v2.1.1  A Bootstring encoding of ...
webmozart/assert                       1.3.0   Assertions to validate me...
zendframework/zend-authentication      2.6.0   provides an API for authe...
zendframework/zend-code                3.3.1   provides facilities to ge...
zendframework/zend-component-installer 2.1.1   Composer plugin for autom...
zendframework/zend-config              3.2.0   provides a nested object ...
zendframework/zend-console             2.7.0   Build console application...
zendframework/zend-crypt               3.3.0   Strong cryptography tools...
zendframework/zend-db                  2.9.3   Database abstraction laye...
zendframework/zend-debug               2.6.0   Safely dump debug informa...
zendframework/zend-developer-tools     1.2.1   Module for developer and ...
zendframework/zend-dom                 2.7.1   provides tools for workin...
zendframework/zend-escaper             2.6.0   Securely and safely escap...
zendframework/zend-eventmanager        3.2.1   Trigger and listen to eve...
zendframework/zend-filter              2.8.0   provides a set of commonl...
zendframework/zend-http                2.8.2   Provides an easy interfac...
zendframework/zend-hydrator            2.4.0   Serialize objects to arra...
zendframework/zend-inputfilter         2.8.2   Normalize and validate in...
zendframework/zend-json                3.1.0   provides convenience meth...
zendframework/zend-loader              2.6.0   Autoloading and plugin lo...
zendframework/zend-mail                2.10.0  Provides generalized func...
zendframework/zend-math                3.1.1   Create cryptographically ...
zendframework/zend-mime                2.7.1   Create and parse MIME mes...
zendframework/zend-modulemanager       2.8.2   Modular application syste...
zendframework/zend-mvc                 3.1.1   Zend Framework's event-dr...
zendframework/zend-paginator           2.8.1   zend-paginator is a flexi...
zendframework/zend-permissions-acl     2.7.0   Provides a lightweight an...
zendframework/zend-permissions-rbac    3.0.1   Provides a role-based acc...
zendframework/zend-router              3.2.0   Flexible routing system f...
zendframework/zend-servicemanager      3.3.2   Factory-Driven Dependency...
zendframework/zend-stdlib              3.2.1   SPL extensions, array uti...
zendframework/zend-test                3.2.0   Tools to facilitate unit ...
zendframework/zend-uri                 2.6.1   A component that aids in ...
zendframework/zend-validator           2.10.2  provides a set of commonl...
zendframework/zend-view                2.10.0  provides a system of help...
zfcampus/zf-api-problem                1.3.0   ZF2 Module providing API-...
zfcampus/zf-apigility                  1.4.0   Apigility module for Zend...
zfcampus/zf-apigility-admin            1.6.0   Apigility Admin module
zfcampus/zf-apigility-admin-ui         1.3.11  Apigility Admin UI module
zfcampus/zf-apigility-documentation    1.3.0   Apigility API documentati...
zfcampus/zf-apigility-provider         1.3.0   Apigility interfaces
zfcampus/zf-asset-manager              1.2.0   Composer plugin for copyi...
zfcampus/zf-composer-autoloading       2.1.0   Sets up Composer-based au...
zfcampus/zf-configuration              1.3.3   Zend Framework module pro...
zfcampus/zf-console                    1.4.0   Library for creating and ...
zfcampus/zf-content-negotiation        1.4.0   ZF Module providing conte...
zfcampus/zf-content-validation         1.6.1   Zend Framework module pro...
zfcampus/zf-deploy                     1.3.0   Deployment tool for Zend ...
zfcampus/zf-development-mode           3.2.0   Zend Framework developmen...
zfcampus/zf-hal                        1.5.0   ZF2 Module providing Hype...
zfcampus/zf-mvc-auth                   1.5.1   ZF2 Module providing Auth...
zfcampus/zf-oauth2                     1.5.0   ZF module for implementin...
zfcampus/zf-rest                       1.5.0   ZF Module providing struc...
zfcampus/zf-rpc                        1.4.0   ZF2 Module for simplifyin...
zfcampus/zf-versioning                 1.3.0   ZF2 Module providing list...
zfr/zfr-cors                           v1.5.0  Zend Framework 2 module t...

Thanks for your answer ;)

weierophinney commented 6 years ago

Okay, my assumption was correct.

Console routing is not enabled by default with zend-mvc. We extracted console routing integration into an optional component, zendframework/zend-mvc-console. You'll need to install that as well using composer require zendframework/zend-mvc-console.

Which leads me to this comment:

And if I ear you well about that deprecation stuff, I would need to pull out a completely new framework only in order to have such simple feature that is the console ?? Seems quite overkill :(

First, we have deprecated zend-mvc-console specifically because we feel mixing console and MVC concerns is a bad idea. Controllers handling console requests need to interact differently than those handling MVC requests (e.g., reporting feedback to users, prompting for more input, etc.). We recommend using a console runner instead, mentioning specifically zf-console (which builds on top of zend-console), and symfony/console.

Regarding that latter option, symfony/console is the de facto standard for developing console applications in PHP. It is a standalone component, and does not require installing the full-stack Symfony framework. One reason we recommend it is because it has a ton of features we've never addressed in zend-console, and which would require a complete rewrite for us to address in any case.

In Expressive, we actually use it in the zend-expressive-tooling component.

We enthusiastically promote using the write tool for the job, including third party tools where they offer more than we do, or where we feel we cannot offer something meaningfully different. This is one of those cases where we feel a third-party tool is a better solution.

Vinnythetrue commented 6 years ago

@weierophinney Thank you very much for such a very comprehensive and complete answer !