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.
Related
I'm just starting off with Zend Framework, and I'm not quite sure what I'm doing wrong with the URI routing.
I'm starting with an initial Zend Framework project in Zend Studio based in my htdocs folder (I'm using Zend Server as well on Windows 7). Everything up to there seems to be working fine getting the index page up (it's running out of the /public/ subdirectory).
But when I try to add a module though, in this case called Users with a controller called Index, and following the instructions in getting that configured, I'm not sure what I should be putting in the URI to get it to route to it's view. I've tried just about every configuration of URI combinations that I can think of (localhost:80/public/users, localhost:80/public/users/index, localhost:80/users, etc)
I'm not getting a routing error, but just a plain 404 page.
Do I need to set the public folder as the root? Or is there something else I need to do to get the routing to work?
~edit in response to bitWorking
It looks like it does automatically add it to the application.config.php. But here is the module.config.php of the Users module
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/index',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => '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(
),
),
),
),
),
),
),
Now I do see where it's guiding you to customize the routes. I've experimented with this as well, but still am not sure what I should set them to. Much closer though.
If you want to call the Index controller in your Users module with /users you have to name the route accordingly:
...
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
---------
...
Else please control the application.config.php. It should look like:
return array(
'modules' => array(
'Application',
'Users',
),
...
So the Url's should look like:
localhost/public/users -> Users/Controller/IndexController/indexAction
localhost/public/users/foo -> Users/Controller/FooController/indexAction
localhost/public/users/foo/bar -> Users/Controller/FooController/barAction
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 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>
I am trying to solve the routing problem I am facing right now with no luck (googled for hours, saw dozens of examples and solved questions but none works for me - the closest one is this Routing Zend Framework 2 Language in url but even this is not working for me).
I have created an SSO application (I mean the authentication part) and now I am porting it to ZF2 app (I have it almost working as I workarounded the router but need now the final routing to be done) where only these types of URLs are possible:
http[s]://domain.com/login/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/login/tdhjsdgbndfnfgdnhd/
http[s]://domain.com/logout/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/info/dthnzdfbdfhgnfsd/
Those login, logout or info parts are not the controllers nor actions but the methods names I then call on SSO service from within my IndexController and indexAction(). The rest part of the URL is the client application hash.
Now here is my router config which only matches the home route and when the other parts (method name [and hash]) are provided, I got 404 from the ZF2 app (so at least I am in the app context).
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'SSO\Controller\Index',
'action' => 'index',
'act' => 'login', // by doing this I am able to workaround the router and work with SSO
'client' => '<my_secret_hash>', // by doing this I am able to workaround the router and work with SSO
),
),
'child_routes' => array(
'action' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '[/:act]',
'defaults' => array(
'act' => 'login',
),
'constraints' => array(
'act' => '(login|logout|info)?', // only login, logout or info are allowed
),
),
'child_routes' => array(
'client' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '[/:client]',
'constraints' => array(
'client' => '[a-zA-Z0-9]+',
),
'defaults' => array(
'client' => '<my_secret_hash>',
),
'may_terminate' => true,
),
),
),
),
),
),
),
);
(only router part of my module.config is provided...)
Now with that router only http[s]://domain.com[/] is matched and either of http[s]://domain.com/(login|logout|info)[/] or http[s]://domain.com/(login|logout|info)/adfbsDVdfbzdbxfbndzxbxfnb[/] matches into A 404 error occured.
Though I try to define the route parts as optional (just for the testing and development purposes), they should by required in the production environment. Anyway, I tried to define them NOT optional too, but didn't work either.
Question: How should I configure the router to match my routes (URLs) defined in the beginning?
Couple of things:
My guess is that you have too many slashes in there -- a child route of / doesn't need to be defined with a slash at the beginning since it's parent already got it
I don't see a reason for 2 nested routes (of course I don't know the rest of your config, so it's a guess)
home route probably should be Literal and able to terminate (again a guess)
there was one unmatched parenthesis (probably 'misspasted' or something)
I suppose this should work for you:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'may_terminate' => true,
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'SSO\Controller\Index',
'action' => 'index',
),
),
'child_routes' => array(
'whateva' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '[:act[/:client]]', // as Sam suggested
'defaults' => array(
'act' => 'login',
'client' => 'fsadfasf32454g43g43543',
),
'constraints' => array(
'act' => '(login|logout|info)',
'client' => '[a-zA-Z0-9]+',
),
),
),
),
),
),
),
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