2016-07-28 7 views
0

Ich habe Zend Framework Version 2 verwendet. Ich folge den Anweisungen von here, aber mein Projekt läuft nicht gut. Hier ist mein Code:ZF2 Console Routing läuft nicht gut

module.config.php

return array(
    'controllers'=> array(
     'invokables' => array(
      'Application\Controller\Index' => 'Application\Controller\IndexController', 
     ), 
    ), 

    'router' => array(
     'routes' => array(

     ) 
    ), 

    // Placeholder for console routes 
    'console' => array(
     'router' => array(
      'routes' => array(
       'list-users' => array(
        'options' => array(
         'route' => 'show [all|disabled|deleted]:mode users [--verbose|-v]', 
         'defaults' => array(
          'controller' => 'Application\Controller\Index', 
          'action'  => 'showusers' 
         ) 
        ) 
       ), 
      ), 
     ), 
    ), 
); 

module.php

public function getConsoleUsage(Console $console) 
    { 
     return array(
      'Finding and listing users', 
      'list [all|disabled] users [-w]' => 'Show a list of users', 
      'find user [--email=] [--name=]' => 'Attempt to find a user by email or name', 

      array('[all|disabled]', 'Display all users or only disabled accounts'), 
      array('--email=EMAIL',  'Email of the user to find'), 
      array('--name=NAME',  'Full name of the user to find.'), 
      array('-w',    'Wide output - When listing users use the whole available screen width'), 

      'Manipulation of user database:', 
      'delete user <userEmail> [--verbose|-v] [--quick]' => 'Delete user with email <userEmail>', 
      'disable user <userEmail> [--verbose|-v]'   => 'Disable user with email <userEmail>', 

      array('<userEmail>' , 'user email'  , 'Full email address of the user to change.'), 
      array('--verbose' , 'verbose mode'  , 'Display additional information during processing'), 
      array('--quick'  , '"quick" operation' , 'Do not check integrity, just make changes and finish'), 
      array('-v'   , 'Same as --verbose' , 'Display additional information during processing'), 

     ); 
    } 

IndexController.php

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

class IndexController extends AbstractActionController 
{ 
    public function indexAction() 
    { 
     return new ViewModel(); 
    } 

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

     return "HOLLLLLLLLLLLLAAAAAAAAAAAA"; // show it in the console 
    } 
} 

Als ich versuchte, es mit dem Befehl promt auszuführen:

C:\webserver\www\program\phpcas\zf2-console>php public/index.php show users 

immer getConsoleUsage() von module.php zeigt, kann mir jemand helfen, wie man es beheben?

Antwort

0

Von meiner (kleinen) Erfahrung scheint es, dass Ihre module.config.php und Ihre Module.php Dateien korrekt sind.

Normalerweise, wenn ich die Konsole verwenden, sind meine Controller etwas anders:

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

// For Cron/Console 
use Zend\Console\Request as ConsoleRequest; 
use Zend\Console\ColorInterface as Color; 

class IndexController extends AbstractActionController 
{ 
    public function indexAction() 
    { 
     return new ViewModel(); 
    } 

    public function showusersAction() 
    { 
     // Initialize variables. 
     $request = $this->getRequest(); 
     $console = $this->getServiceLocator()->get('console'); 

     // Make sure that we are running in a console and the user has not tricked our 
     // application into running this action from a public web server. 
     if (!$request instanceof ConsoleRequest) { 
      throw new \RuntimeException('You can only use this action from a console!'); 
     } 

     // Retrieve values from value flags using their name. 
     $verbose = (bool)$request->getParam('verbose', false) || (bool)$request->getParam('v', false); // default false 
     /* ...your other flags... */ 

     /* ...do what you want to do... */ 
     // Let's confirm it works 
     $console->writeline("Groovy Baby!"); 
+0

Thx für Ihre Hilfe. Es funktioniert.. –

Verwandte Themen