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>
Related
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.
So I've been following the fairly straightforward zend 2 skeleton example "album". I followed every step to the teeth and yet I cannot escape the 404 Error - requested URL could not be matched by routing whenever I input http://hostname/album for indexing, or http://hostname/album/add for adding, etc.
Naturally I looked into the routing found in the module.config.php file:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
Everything here looked fine, so I looked into the Module.php where the module.config.php is getting loaded from:
<?php
namespace Album;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
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';
}
}
Again, everything looks fine here. Now I thought maybe the problem is that I didn't include the Album module in the application.config.php file (comments removed):
<?php
return array(
'modules' => array(
'Application',
'Album',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
'config_glob_paths' => array(
'config/autoload/{{,*.}global,{,*.}local}.php',
),
);
However it is included. I also have the AlbumController.php and the view (.phtml) files exactly where they should be. I double checked the paths multiple times yet the routes still do not work. Any ideas? Any suggestion would be appreciated.
PS - I am using a Ubuntu 14.04 Virtual Box.
EDIT
Here's the directory structure for the application: (I'm just listing the relevant files/folders to make it more readable)
ZendSkel
public
index.php
config
application.config.php
module
Application
Album
Module.php
config
module.config.php
src
Album
Controller
AlbumController.php
Model
Form
view
album
album
index.phtml
add.phtml
edit.phtml
delete.phtml
Also, I am using virtual host with apache2.2.
For anyone, who is still looking for solution of this problem,
Clearing the data/cache folder, allowed routing to work as expected.
Like #Mayank Awasthi said:
Clearing the data/cache folder, allowed routing to work as
expected.
This applies to Zend AND Laminas.
If the issue is not cache related, check your config and routing files!
If you follwoing the tutorial in the official page step by step and it shows 404. make sure 1- stop the serve,
2- enter "composer development-enable".
3- enter "composer serve"
and then it should work. The tutorial does mention "composer development-enable" but it doesnt mention it in the right order, reason why a lot of people keep getting the 404
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.
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
I've got a strange situation...
After the creation of the ZF2 SkeletonApplication I created an extra Module called Authentication with an AuthController and a LoginAction also in the view directory "authentication/auth" i placed a login.phtml.
When i run the app i get an error
Zend\View\Renderer\PhpRenderer::render: Unable to render template "authentication/auth/login"; resolver could not resolve to a file
The strange thing is that when I place the complete folder "authentication/auth/login.phtml" in the Standard Application Module View Folder it finds it.
So Zend is looking in the wrong directory.
This is my module.config.php (Authentication Module).
return array(
'router' => array(
'routes' => array(
'authentication' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/authentication/login',
'defaults' => array(
'controller' => 'Authentication\Controller\Auth',
'action' => 'login',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Authentication\Controller\Auth' => 'Authentication\Controller\AuthController',
),
),
'viewmanager' => array(
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
)
);
This is the AuthController
namespace Authentication\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AuthController extends AbstractActionController
{
public function loginAction()
{
return new ViewModel();
}
}
I hope someone can point me in the right direction.
The complete path cannot be resolved by zf2. The viewManager is using your template pathStack to find the relative view. In your example, the viewManager is looking for this file :
DIR . '/../view/authentication/auth/login.phtml
In other way, you can add to your viewManager a templateMap like this :
'view_manager' => array(
'template_map' => array(
'authentication/auth/login' => __DIR__ . '/../view/where/you/want.phtml',
)
);
change your config:
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
I am guessing you are a level too far back..
If you config is here:
Authentication/config/module.config.php
then you only want to go back a single level, then into your view directory. Your code would take you back a level higher, into the modules directory.