ZF2 控制台路由 运行 不好
ZF2 Console routing doesn't run well
我使用 Zend Framework 版本 2。我按照 here 的说明进行操作,但我的项目 运行 效果不佳。
这是我的代码:
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
}
}
当我尝试使用命令 promt 运行 时:
C:\webserver\www\program\phpcas\zf2-console>php public/index.php show users
总是显示 Module.php 的 getConsoleUsage()
,谁能帮帮我,怎么解决?
根据我的(小)经验,您的 module.config.php
和 Module.php
文件似乎是正确的。
通常,当我使用控制台时,我的控制器略有不同:
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!");
我使用 Zend Framework 版本 2。我按照 here 的说明进行操作,但我的项目 运行 效果不佳。 这是我的代码:
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
}
}
当我尝试使用命令 promt 运行 时:
C:\webserver\www\program\phpcas\zf2-console>php public/index.php show users
总是显示 Module.php 的 getConsoleUsage()
,谁能帮帮我,怎么解决?
根据我的(小)经验,您的 module.config.php
和 Module.php
文件似乎是正确的。
通常,当我使用控制台时,我的控制器略有不同:
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!");