I'm trying to make a crone app in my zend framework application. The problem is when I try to running it. It shows me the following error and the command is:
Command: php public/index.php cron full
Result: Reason for failure: Invalid arguments or no arguments provided
module.config.php:
array(
'controllers' => array(
'invokables' => array(
'Sync\Controller\Cron' => 'Sync\Controller\CronController',
//'Sync\Controller\Index' => 'Sync\Controller\IndexController',
),
),
'console' => array(
'router' => array(
'routes' => array(
/*'user-reset-password' => array(
'options' => array(
'route' => 'user resetpassword [--verbose|-v] <userEmail>',
'defaults' => array(
'controller' => 'Sync\Controller\Index',
'action' => 'password'
)
)
),*/
'cron' => array(
'options' => array(
'route' => 'cron full',
'defaults' => array(
'controller' => 'Sync\Controller\Cron',
'action' => 'full'
),
),
),
)
)
)
)
CronController.php
class CronController extends AbstractActionController{
public function fullAction()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
return("hi");
}
public function centerAction()
{
}
}
Module.php
public function getConsoleUsage(Console $console)
{
return array(
// Describe available commands
/*$console->colorize('User resetpassword [--verbose|-v] EMAIL ->Reset password for a user', Color::YELLOW),
// Describe expected parameters
array($console->colorize('EMAIL', Color::GREEN), 'Email of the user for a password reset'),
array('--verbose|-v', '(optional) turn on verbose mode'),*/
$console->colorize('Cron [full|center]', Color::YELLOW),
array($console->colorize('cron full', Color::GREEN), 'Execute full cron job'),
array($console->colorize('cron center', Color::GREEN), 'Execute center cron job'),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
The error
"Reason for failure: Invalid arguments or no arguments provided"
will happen if the console route has not been registered.
Is that your full module.config.php? I assume you just excluded the return statement for the array from the code snippet and it really is there in the module.config.php in your application?
Also is this in a separate module? Have you definitely included the module in config/application.config.php
as everything seems ok with the code
Related
I am using ZfcAdmin module for ZF2 (https://github.com/ZF-Commons/ZfcAdmin/) and I can't go through using my own controller.
According to the module documentation (https://github.com/ZF-Commons/ZfcAdmin/blob/master/docs/2.Routes.md) I should be able to use my own controller after simply adding this snippet from docs in my new module routes config, however it just cause a redirect to main page as it didn't find the route. So I've added this part:
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
Which results in error:
Exception: Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "admincontrolleradmin(alias: Admin/Controller/Admin)" via invokable class "Admin\Controller\AdminController"; class does not exist in /var/www/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:240
Module.php
<?php
namespace Admin;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
)
);
}
}
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'options' => array(
'defaults' => array(
'controller' => 'Admin/Controller/Admin',
'action' => 'test',
),
),
),
),
),
);
module/Admin/src/Admin/AdminController.php
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class AdminController extends AbstractActionController
{
public function testAction()
{
echo('test');
die();
}
}
I am 99% sure it's my fault, not zfcadmin itself. Even though, I have no idea where I made a mistake while creating my own controller which can't be found.
Zend 2 cannot find your "AdminController.php".
Your best option is to change "AdminController.php" to be under "module/Admin/src/Controller/", not "module/Admin/src/Admin/".
I am new in Zend Frameworkâ 2 . i have downloaded zend-skeleton application its working fine.but when i created new module of album & calling album route page not found error(404) occurs .
I have tried to apply many types of zf2 routes but same error message.
This is the error page which is being displayed again & again when i access album route :
I don't know why this error is being occurred? Kindly suggest me any solution?
Try to check your Module.php it should look something like this
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
especially check namespace in all module files
your module.config.php should look like this
'controllers' => array(
'invokables' => array(
'index' => 'Album\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'literal',
'options' => array(
'route' => '/album',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
I hope, it will help you.
I'm new with Zend Framework, i used to use framework CI and Laravel but in my project i should use ZF2. Honestly, i don't get it how to building app with ZF2. I followed the instructions from here : https://www.youtube.com/watch?v=OYkVHiXeGeY
but i got some error.
Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "csnusercontrolleruser(alias: CsnUser\Controller\User)" via invokable class "CsnUser\Controller\UserController"; class does not exist
my strucktur folder:
:: module
->CsnUser
>>config
->module.config.php
>>src
->CsnUser
->Controller
->UserController.php
>>view
->csn-user
->user
->index.phtml
>>Module.php
Module.php
<?php
namespace CsnUser;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespace' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'CsnUser\Controller\User' => 'CsnUser\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'csn_user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/csn-user',
'defaults' => array(
'__NAMESPACE__' => 'CsnUser\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(),
),
),
),
),
),
),
'view_manager' => array(
/*'template_map' => array(
'layout/Auth' => __DIR__ . '/../view/layout/xxxx.phtml',
),*/
'template_path_stack' => array(
'csn_user' => __DIR__ . '/../view'
),
),
);
UserController.php
<?php
namespace CsnUser\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class UserController extends AbstractActionController{
public function indexAction()
{
return new ViewModel();
}
}
index.phtml
<h1>Welcome</h1>
Can anyone help me ?
Your getAutoLoaderConfig() method is incorrect.
Try this once:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
Pay attention to the key namespaces in the array.
In your instruction video at 4:40 they also use the plural :)
I solved the problem by adding a string to composer.json:
"autoload": {
"psr-0": {
"MyModule": "module/MyModule/src/"
}
}
and then, in the console, I navigated to the project folder and ran:
php composer.phar update
I'm trying to make a cron app in my zf2 project but it gave me always the following message :
Reason for failure: Invalid arguments or no arguments provided
My code is this:
module.config.php
'controllers' => array(
'invokables' => array(
'Sync\Controller\Cron' => 'Sync\Controller\CronController',
'Sync\Controller\Index' => 'Sync\Controller\IndexController'
),
),
'console' => array(
'router' => array(
'routes' => array(
'user-reset-password' => array(
'options' => array(
'route' => 'user resetpassword [--verbose|-v] <userEmail>',
'defaults' => array(
'controller' => 'Sync\Controller\Index',
'action' => 'password'
)
)
),
'cron' => array(
'options' => array(
'route' => 'cron [full|center]',
'defaults' => array(
'controller' => 'Sync\Controller\Cron',
'action' => 'full'
)
)
)
)
)
)
CronController.php
class CronController extends AbstractActionController
{
public function fullAction()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
return("hi");
}
public function centerAction()
{
}
}
Given that a matching php binary is in the path you can then call from your application folder:
php public/index.php cron full
or just
php public/index.php cron (since the route defaults to the fullAction)
I'm trying to get started with ZF2 and I have a problem when I writting code from tutorial (on ZF website). My code:
Module.php:
<?php
namespace About;
class About
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
?>
config/module.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'About\Controller\About' => 'About\Controller\AboutController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/about[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'About\Controller\About',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'about' => __DIR__ . '/../view',
)
),
);
Problem is:
Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (About) could not be initialized.' in /var/www/zend2/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php on line 175
Why it's shown on start? (in my project: /var/www/zend2/). If I remove module declaration from application.config.php it works okay. What is my problem? :/
Ouch, solved!
In Module.php class must be named Module, not own name...
When having this issue with PSR-4 loading you could also check whether the module name and path to the folder are correct for autoloading inside your composer.json file:
"autoload": {
"psr-4": {
"YourModule\\": "module/YourModule/src/",
... other modules ...
}
},
And then after fixing things run:
composer update
I believe your class indexes are outdated, you can follow this command:
composer install -o