2017-03-07 145 views
1

我正在Symfony 3网站上工作,我需要用cron作业调用我的网站的URL。使用cron作业触发URL

我的网站托管在OVH,我可以配置我的cron作业。

现在,我已经设置了命令:./demo/Becowo/batch/emailNewUser.php

emailNewUser.php内容:

<?php 

header("Location: https://demo.becowo.com/email/newusers"); 

?> 

在日志中我有:

[2017年3月7日08: 08:04] ## OVH ## END - 2017-03-07 08:08:04.448008 exitcode:0

[2017-03-07 09:08:03] ## OVH ## START - 2017-03 -07 09:08:03 0.988105执行:/usr/local/php5.6/bin/php /homez.2332/coworkinwq/./demo/Becowo/batch/emailNewUser.php

但邮件没有发送。 我应该如何配置我的cron作业来执行此URL? 或者我应该直接打电话给我的控制器?怎么样 ?

+3

这会是一个好得多的想法,并且更安全一些,可以写一个symfony命令来做你想做的事情,然后调用它。 – DevDonkey

+0

像DevDonkey,我会做同样的,如果你的主机允许你执行命令。 –

回答

0

如上所述,您应该使用symfony commaad。这是你的一个例子。

注意:虽然它工作,你可以随时改善这个例子。特别是命令调用端点的方式。

控制器服务定义:

services: 
    yow_application.controller.default: 
     class: yow\ApplicationBundle\Controller\DefaultController 

控制器本身

namespace yow\ApplicationBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 
use Symfony\Component\HttpFoundation\Response; 

/** 
* @Route("", service="yow_application.controller.default") 
*/ 
class DefaultController 
{ 
    /** 
    * @Method({"GET"}) 
    * @Route("/plain", name="plain_response") 
    * 
    * @return Response 
    */ 
    public function plainResponseAction() 
    { 
     return new Response('This is a plain response!'); 
    } 
} 

命令服务定义

services: 
    yow_application.command.email_users: 
     class: yow\ApplicationBundle\Command\EmailUsersCommand 
     arguments: 
      - '@http_kernel' 
     tags: 
      - { name: console.command } 

命令本身

namespace yow\ApplicationBundle\Command; 

use Symfony\Component\Console\Command\Command; 
use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpKernel\HttpKernelInterface; 

class EmailUsersCommand extends Command 
{ 
    private $httpKernel; 

    public function __construct(HttpKernelInterface $httpKernel) 
    { 
     parent::__construct(); 

     $this->httpKernel = $httpKernel; 
    } 

    protected function configure() 
    { 
     $this->setName('email:users')->setDescription('Emails users'); 
    } 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $request = new Request(); 
     $attributes = [ 
      '_controller' => 'yow_application.controller.default:plainResponseAction', 
      'request' => $request 
     ]; 
     $subRequest = $request->duplicate([], null, $attributes); 

     $response = $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); 

     $output->writeln($response); 
    } 
} 

测试

$ php bin/console email:users 
Cache-Control:  no-cache, private 
X-Debug-Token:  99d025 
X-Debug-Token-Link: /_profiler/99d025 

This is a plain response! 
1.0 
200 
OK 
+0

嗨!感谢您的回答。你的意思是我可以用cron任务调用命令'php bin/console email:users'? – Olivia

+0

是的,这是正确的。 – BentCoder

+0

类似'*/5 * * * * cd/path/to/your/app && php bin/console --env = prod email:users' should do。我猜你知道'--env'标志的含义。 – BentCoder

1

OK,终于它的工作原理!

下面是步骤我跟着别人:

1 /你需要一个控制器可以发送电子邮件:

由于控制器将通过命令来调用,你需要injec一些服务

EM:实体管理器刷新数据

邮件:访问swiftMailer服务发送的电子邮件

模板:访问TWIG服务,在电子邮件正文中使用模板

MemberController.php

<?php 
 

 
namespace Becowo\MemberBundle\Controller; 
 

 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
 
use Symfony\Component\HttpFoundation\Request; 
 
use Becowo\CoreBundle\Form\Type\ContactType; 
 
use Becowo\CoreBundle\Entity\Contact; 
 
use Doctrine\ORM\EntityManager; 
 
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; 
 

 
class MemberController extends Controller 
 
{ 
 
    private $em = null; 
 
    private $mailer = null; 
 
    private $templating = null; 
 
    private $appMember = null; 
 

 
    public function __construct(EntityManager $em, $mailer, EngineInterface $templating, $appMember) 
 
    { 
 
     $this->em = $em; 
 
     $this->mailer = $mailer; 
 
     $this->templating = $templating; 
 
     $this->appMember = $appMember; 
 
    } 
 

 
    
 

 
    public function sendEmailToNewUsersAction() 
 
    { 
 
    // To call this method, use the command declared in Becowo\CronBundle\Command\EmailNewUserCommand 
 
    // php bin/console app:send-email-new-users 
 

 
    \t $members = $this->appMember->getMembersHasNotReceivedMailNewUser(); 
 
    \t $nbMembers = 0; 
 
    \t $nbEmails = 0; 
 
    \t $listEmails = ""; 
 
    
 
    \t foreach ($members as $member) { 
 
    \t \t $nbMembers++; 
 
    \t \t if($member->getEmail() !== null) 
 
    \t \t { 
 
    \t \t \t $message = \Swift_Message::newInstance() 
 
\t   ->setSubject("Hello") 
 
\t   ->setFrom(array('[email protected]' => 'Contact Becowo')) 
 
\t   ->setTo($member->getEmail()) 
 
      ->setContentType("text/html") 
 
\t   ->setBody(
 
\t    $this->templating->render(
 
\t     'CommonViews/Mail/NewMember.html.twig', 
 
\t     array('member' => $member) 
 
\t   )) 
 
      ; 
 

 
\t  \t $this->mailer->send($message); 
 
\t  \t $nbEmails++; 
 
\t  \t $listEmails = $listEmails . "\n" . $member->getEmail() ; 
 

 
\t  \t $member->setHasReceivedEmailNewUser(true); 
 
\t  \t 
 
\t \t \t $this->em->persist($member); 
 
    \t \t } 
 
    \t } 
 
     $this->em->flush(); 
 

 
    \t $result = " Nombre de nouveaux membres : " . $nbMembers . "\n Nombre d'emails envoyes : " . $nbEmails . "\n Liste des emails : " . $listEmails ; 
 
    
 

 
    \t return $result; 
 
    } 
 

 
}

2 /叫你控制器作为服务

应用/config/services.yml

app.member.sendEmailNewUsers : 
 
     class: Becowo\MemberBundle\Controller\MemberController 
 
     arguments: ['@doctrine.orm.entity_manager', '@mailer', '@templating', '@app.member']

3 /创建一个控制台命令来调用你的控制器

文件:http://symfony.com/doc/current/console.html

YourBundle /命令/ EmailNewUserCommand.php

<?php 
 

 
namespace Becowo\CronBundle\Command; 
 

 
use Symfony\Component\Console\Command\Command; 
 
use Symfony\Component\Console\Input\InputInterface; 
 
use Symfony\Component\Console\Output\OutputInterface; 
 
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 
 

 
class EmailNewUserCommand extends ContainerAwareCommand 
 
{ 
 
    protected function configure() 
 
    { 
 
     
 
     // the name of the command (the part after "php bin/console") 
 
     $this->setName('app:send-email-new-users') 
 
\t \t \t ->setDescription('Send welcome emails to new users') 
 
    \t ; 
 
    } 
 

 
    protected function execute(InputInterface $input, OutputInterface $output) 
 
    { 
 
    \t // outputs a message to the console followed by a "\n" 
 
     $output->writeln('Debut de la commande d\'envoi d\'emails'); 
 

 
     \t // access the container using getContainer() 
 
     $memberService = $this->getContainer()->get('app.member.sendEmailNewUsers'); 
 
     $results = $memberService->sendEmailToNewUsersAction(); 
 

 
     $output->writeln($results); 
 
    } 
 
}

4测试你的命令!

在控制台中,打电话给你的命令:php bin/console app:send-email-new-users

5 /创建一个脚本来运行命令

文件(法国):http://www.christophe-meneses.fr/article/deployer-son-projet-symfony-sur-un-hebergement-perso-ovh

..Web /批号/ EmailNewUsers .sh

#!/bin/bash 
 

 
today=$(date +"%Y-%m-%d-%H") 
 
/usr/local/php5.6/bin/php /homez.1111/coworkinwq/./demo/toto/bin/console app:send-email-new-users --env=demo > /homez.1111/coworkinwq/./demo/toto/var/logs/Cron/emailNewUsers-$today.txt

这里花了我一些时间才得到正确的脚本。

保重php5.6的:它必须匹配OVH

你的PHP版本不要忘记上传斌/ console文件服务器

HOMEZ上。XXXX /名称必须配合你的配置(我发现OVH矿,然后在日志)

重要提示:当你上传的文件在服务器上,添加执行权(CHMOD 704)

6 /在OVH

创建cron任务与命令调用脚本:./demo/Becowo/web/Batch/EmailNewUsers.sh

语言:其他

7 /等待!

您需要等待下一次运行。然后查看OVH cron日志,或通过.sh文件中的命令创建您自己的日志

花了我好几天才搞定它.. 享受!

+0

显然现在问题已经解决了,所以下一步有关这篇文章的内容是什么? – BentCoder