zoukankan      html  css  js  c++  java
  • Phalcon下命令行应用(command line applications)

    今天打算在phalcon项目下加入一个定时任务,来发送邮件。

    看了官网的例子,配置和运行都少许杂乱,还有个bug,见备注, 我这边主要讲解如何将command line 的配置独立出来,如果初上手的同学,建议先运行官网第一个例子,成功后,再实践一下官网第二个例子,出现报错,就看我备注,然后再看我这如何将配置独立处理。

    借鉴思想:主要借鉴Yii框架中,类似功能commands的配置console.php。

    第一步:在app下的tasks文件夹下建三个文件:

    cl_config.php    //直接把app/config/config.php内容copy进去

    cl_loader.php   //同样把app/config/loader.php内容copy进去,注意要有所取舍,比如modelsDir,用数据库时用,但controllersDir就没啥用,可以注释掉

    cl_services.php //也同样copy进去,但$di不需要重新生成,直接用好了,把没用的都注释掉,一般只保留$di->set('db'.... 那段

    第二步:修改app下的cli.php文件

    if(is_readable(APPLICATION_PATH . '/tasks/cl_config.php')) {      //导入cl_config.php
    $config = include APPLICATION_PATH . '/tasks/cl_config.php';
    $di->set('config', $config);
    }
    include APPLICATION_PATH. "/tasks/cl_loader.php";         //现在引入autoloader,
    include APPLICATION_PATH. "/tasks/cl_services.php";      //引入cl_services

    ok啦,,,,

    备注:

    cli.php里的一部分配置

    $arguments = array();
    foreach($argv as $k => $arg) {
    if($k == 1) {
    $arguments['task'] = $arg;
    } elseif($k == 2) {
    $arguments['action'] = $arg;
    } elseif($k >= 3) {
    $arguments[] = $arg;  //需改成$arguments['params'][] = $arg;否则,在命令行带上参数运行时,会报错,如下
    }
    }

    运行:$ php app/cli.php main test ff ss ww mm

    报错内容:

    PHP Catchable fatal error:  Argument 1 passed to MainTask::testAction() must be of the type array, string given in /Users/liangzhongyuan/Sites/library/app/tasks/MainTask.php on line 24

     Catchable fatal error: Argument 1 passed to MainTask::testAction() must be of the type array, string given in /Users/liangzhongyuan/Sites/library/app/tasks/MainTask.php on line 24

     

    相关连接:

    http://www.myleftstudio.com/reference/cli.html //官网讲解

    1、以专家为榜样,不必自己重新探索
    2、解构技能,找出实现80%效果的那20%
    3、不要一心二用
    4、练习练习再练习!然后获得即时反馈
    5、坚持,不要在低谷期放弃
  • 相关阅读:
    npm升级package.json依赖包到最新版本号
    vue中 父子组件的通讯
    vue组件开发
    vue模拟后端获取数据——json-server与express
    vue-cli 2.x 搭建项目
    python socket编程
    python异常处理
    python反射
    python特殊成员函数
    Executor ExecutorService Executors
  • 原文地址:https://www.cnblogs.com/zhongyuan/p/3819075.html
Copyright © 2011-2022 走看看