Unable to use console in ZF2 - php

I'm trying to access a function from my controller, using the console. It's a small application, so i only have one controller, 'IndexController'. The action i'm trying to reach is the 'buildSchemeAction'.
As i understand, you have to add routes in your "module.config.php" file, which i did as followed :
return array(
'console' => array (
'router' => array (
'routes' => array (
'build-scheme' => array (
'options' => array (
'route' => 'build-scheme',
'defaults' => array (
'controller' => 'Application\Controller\Index',
'action' => 'build-scheme'
)
)
)
)
)
), ..//
My Controller function looks like this :
public function buildSchemeAction()
{
// logic here
}
When i enter the following in console :
php index.php build-scheme
i get :
Zend Framework 2.2.5 application
Usage:
Reason for failure: Invalid arguments or no arguments provided
I've been searching the web, but can't find anything. Am i missing something?

I just found out why it didn't work. I had my console routes at the top of my module.config file, while this was at the very bottom :
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
../
),
),
),
Totally overlooked it, since it was there by default.

Related

Zendframework - Adding an action to existing controller results in permission denied

I'm using Zendframework (2.3), I'm struggling to understand what I'm doing wrong when attempting to create a new action and view on an existing controller. I've read some relevant documentation but still fail to see what I'm missing.
Currently the controller basically defaults to a single action (main), I would like to add an additional one.
EG:
/list-item/main => // Existing Route
/list-item/add => // New Route I would like to add.
This is how my module.config.php looks:
return array(
'router' => array(
'routes' => array(
'list-item' => array(
'type' => 'segment',
'options' => array(
'route' => '/list-item[/:action][/:id][/:id1][/:id2][/:id3][/:id4][/:id5]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'ListItem',
'action' => 'main',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'ListItem' => 'ListItem\Controller\ListItemController',
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
If I can read this configuration correctly, the actual action name is optional, but the module will default to the main action. Yet it is flexible to allow for any action to be attempted.
So, I proceeded to add a new public function into the ListItemController, assumed this is how the convention works:
public function addAction() {
return new ViewModel();
}
And with it a new view file into the module's views folder, in fact right next to main.phtml but called add.phtml.
But when I attempt to access the route /list-item/add I only get a permission denied. Funny, because the actual status is 200. But I have no other information. I honestly don't even know if this is a ZF thing, I can only assume.
Also, I'm using php -S 0.0.0.0:8080 -t public/ in case the web server might have something to do.
Thanks in advance, any help will be greatly appreciated.

Zend Framework 1 Routing * Params in Adminurl

I got a problem solving an variable Adminurl in Zend Framework 1.
The Route looks fine and should work, it do except if i give parameters.
Thanks for anyone who could help.
Why this will not work?
$adminpath is simple valid string.
'resources' => array(
'router' => array(
'routes' => array(
'backend' => array(
'route' => $adminpath.'/:module/:controller/:action/*'
)
)
)
),
It returns:
http://localhost/cms/admin/AppB/update/activate/moduleName/AppCm
An error occurred
Page not found
Exception information:
Message: Invalid controller specified (admin)
Stack trace:
Request Parameters:
array (
'controller' => 'admin',
'action' => 'AppB',
'update' => 'activate',
'moduleName' => 'AppCm',
'module' => 'App',
)
You have two intersected routes. They are both can parse this URL, but someone parse first, and stops an URL-recognition process. Default route parse URL first and returns
'module' => 'App',
'controller' => 'admin',
'action' => 'AppB',
Try to put defaultRoute initialization
/* #var Zend_Controller_Router_Rewrite $router */
$router->addDefaultRoutes();
/*init of '/:module/:controller/:action/*' route*/
after adding of all other routes .
Sometimes it is important to regulate route priority directly from config. To do this, you can add priority parameter in a routes config, for example:
'routes' => array(
'backend' => array(
'route' => $adminpath.'/:module/:controller/:action/*',
'priority' => 555
),
)
And sort a config by priority before the routes config would add to router ( $router->addConfig call ). In ZF2 route priority param is native.

Zend 2 run from CLI uses HTTP route instead of console route

I am currently working on a zend 2 project and created a console module that i will use with cronjobs. So far i created a standalone project and everything runs fine locally on my working station. Now i uploaded it to my webproject and activated the module. Now i try to run the controller ( route "message" ) but it doesn't use the console route ... it puts out the html that is generated if you call the website with a browser.
php public/index.php message
Any idea what i am missing ? It works locally in a standalone project but not on the server. Could there be any complications with the other modules on the web project ?
The new module config /module/Console/config/module.config.php:
// This lines opens the configuration for the console routing
'console' => array(
'router' => array(
'routes' => array(
'messagecron' => array(
'type' => 'simple',
'options' => array(
'route' => 'message',
'defaults' => array(
'controller' => 'Console\Controller\Message',
'action' => 'send'
)
)
),
)
)
)
It seems that it is using the basic http route from .../module/Application/config/module.config.php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Basic\Controller\Index',
'action' => 'index',
),
),
),
),
),
The problem belongs to the server ( 1&1 managed ) i use ... there are two ways of running php.
1) php public/index.php message
2) php-cli public/index.php message
The solution is, to use the "-cli" for commandline execution.

How to begin using the cron job in zend framework 2

i want to send an email at a specific time of the day ( 9am ).
I saw that i have to use cron jobs. I saw a lot of different things how to work with this, but it seems really difficult. Can somebody explain to me how to do this ?
Thanks in advance.
Create a MailController with an sendMailAction() inside the app/root module.
Add a console route to your app/root module's module.config.php:
array('router' => array(
'routes' => array(..)),
'console' => array(
'router' => array(
'routes' => array(
'cronroute' => array(
'options' => array(
'route' => 'sendemail',
'defaults' => array(
'controller' => 'Root\Controller\Console',
'action' => 'sendemail'
)
)
)
)
)
)
);
Execute this in Terminal
$ cd product path php public/index.php sendemail

How to run cron job with zend framework 2

I have application built in Zend Framework 2. I would like to set cron job for updating my products. I know scripts such as this should be run from outside of public folder, but unfortunately my script in cron needs to use framework files.
How can I do this?
The only way I figured out is to run script from outside of public folder then add some hash or password and redirect to
www.domain.com/cron/test
So I will have all framework functionality.
Will it be secure? Maybe there is a other way?
I strongly recommend to use CLI for such requirement.
Create a ConsoleController with an updateAction() inside the application module.
Add a console route to your application module's module.config.php:
array(
'router' => array(
'routes' => array(
...
)
),
'console' => array(
'router' => array(
'routes' => array(
'cronroute' => array(
'options' => array(
'route' => 'updateproducts',
'defaults' => array(
'controller' => 'Application\Controller\Console',
'action' => 'update'
)
)
)
)
)
)
);
Now open the terminal and
$ cd /path/to/your/project
$ php public/index.php updateproducts
Thats all. Hope it helps.
I found the solution at collabnet (Which is now dead).
I am copying the solution here as ColabEdit sometimes removes posts:
<?php
/*
Cron directory setup:
Cron
config
module.config.php
src
Cron
Controller
IndexController.php
autoload_classmap.php
Module.php
NOTES: Remember to include the Cron module in the main config file (trunk/config/application.config.php)
Once you have the route in place, write your cron and call it from your webhost cron manager.
*/
// Cron/config/module.config.php
return array(
// Placeholder for console routes
'controllers' => array(
'invokables' => array(
'Cron\Controller\IndexController' => 'Cron\Controller\IndexController'
),
),
'console' => array(
'router' => array(
'routes' => array(
//CRON RESULTS SCRAPER
'my-first-route' => array(
'type' => 'simple', // <- simple route is created by default, we can skip that
'options' => array(
'route' => 'hello',
'defaults' => array(
'controller' => 'Cron\Controller\IndexController',
'action' => 'index'
)
)
)
),
),
),
);
<?php
// Cron/src/Cron/Controller/IndexController.php
namespace Cron\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionControlle
{
public function indexAction()
{
echo "hello";
echo "\r\n";
}
}
From the console navigate to trunk (or public_html) (the directory before public) and run:
path/to/trunk>php public/index.php hello
hello
path/to/trunk>

Categories