How to begin using the cron job in zend framework 2 - php

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

Related

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 remove route on overrided module?

I added zfcUser module to my project via Composer and overrided it in the module ZfcUserOverride. I want trailing slash work, so I added route in overrided module.
zfcUserOverride file module.config.php contents below:
<?php
$config = array(
'view_manager' => array(
'template_path_stack' => array(
'zfcuser' => __DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'zfcuser' => 'ZfcUserOverride\Controller\UserController',
),
)
);
$config['router']['routes']['zfcuser']['child_routes']['trailing_slash'] = array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
);
return $config;
I added new path, everythin is working correct.
But what if I want remove route? How to do this? I need somethink like:
$config['router']['routes']['zfcuser']['child_routes']['login'] = null;
Help please. Thank you.
In zfcUserOverride you will need to override the route config rather than add a new one.
This can easily be done by using the same array key when defining the routes.
For example; should I wish to modify the login route to allow the extra slash I would use this:
// zfcUserOverride/config/module.config.php
'router' => array(
'routes' => array(
'zfcuser' => array(
'child_routes' => array(
'login' => array(
'type' => 'Segment',
'options' => array(
'route' => '/login[/]',
),
),
),
),
),
);
Internally ZF2 will combine/merge all module configuration into one complete array using array_replace_recursive(). Matching configuration keys will therefore be replaced by modules that have loaded after.
So you will also need to ensure that you have it correctly configured in application.config.php
array(
'modules' => array(
//...
'ZfcUser',
'ZfcUserOverride', // Loads after
// ...
),
);
Here the answer.
#Sharikov Vladislav, I want to say something to you.
In this question I answered to your question and you choose the correct answer to somebody that just update his answer with my content 10 hours later.
I do not want to start a flame war, what I ask is just to be correct to whom used its time to help you.
And also I think you must use search engines prior to post here, you are asking a question for every single step of your development process and it is clear you are putting no effort on searching a solution by yourself.
Just sayin..

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>

Using route in Zend Framework 2

i need to route urls like in zf1.
in particular i need that these urls will be automatically redirect to appropriate actions without specify a new route every time.
/site/getData
/site/getData?param=5&par2=test
/site/getOther
...
So a segment route doesn't work, i've tried a Literal route but i can't reach a working solutions.
Anyone can help me?
Thanks a lot
This should be solved by a pretty default segment route like the one provided in the documentation.
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:controller[/:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
),
'defaults' => array(
'controller' => 'default-controller-alias',
'action' => 'index',
),
)
Now if you set up your controller names like the following:
'controllers' => array(
'invokables' => array(
'sites' => 'Namespace\Controller\SitesController',
'other' => 'Namespace\Controller\OtherController'
Then you should be able to achieve exactly what you want. And to create params to your route, you simply use the ViewHelper correctly ;)
$this->url('routename',
array(
'controller' => 'site',
'action' => 'getData'
),
array (
'query' => array(
'param1' => 'foo',
'param2' => 'bar',
'paramN' => 'baz',
)
)
)

Zend Framework 2 Console Routes

I'm trying to define some console routes for my ZF2 application as described here http://packages.zendframework.com/docs/latest/manual/en/modules/zend.console.routes.html
in the module config I have:
'console' => array(
'router' => array(
'routes' => array(
'user-set-password' => array(
'options' => array(
'route' => 'user password <username> <password>',
'defaults' => array(
'controller' => 'User\Profile',
'action' => 'setpassword'
),
),
),
),
),
),
but it seems to never match the route as it always prints the usage information. also simple routes like just 'test' won't be matched.
(when I write some crap into the route parameter, the execution fails with an Zend\Mvc\Router\Exception\InvalidArgumentException so it recognizes the console route when loading the module)
is it my fault or maybe a bug in the latest zf2 version?
I just found the solution in an inconsistent interface for the route definitions:
it works if you provide the following schema for the controller:
'controller' => 'User\Controller\Profile'
would be better to be able to define it in the same way as http routes:
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'Profile',
'action' => 'setpassword',
),
just opened an issue for that: http://framework.zend.com/issues/browse/ZF2-515

Categories