2012-03-07 89 views
0

我很好奇Zend是否有一个可以使用shell的组件。例如,我想要执行如下的shell命令:使用Zend框架的shell

mysqldump --compact --uroot --ppass mydatabase mydable >test.sql 

from a controller。

如果没有,你知道如何从Zend Framework中的表转储数据吗?

更新: 我发现这里http://www.zfsnippets.com/snippets/view/id/68

+1

你不能用香草PHP做它吗?我的意思是PHP代码在控制器中非常受欢迎。 您可能需要查看反引号操作符shell_exec(),system()和其他相关函数。 您希望使用专用的组件访问shell是否有任何特定的好处? – masnun 2012-03-07 16:23:46

+0

我在这里找到了一个方法http://www.zfsnippets.com/snippets/view/id/68 – zuzuleinen 2012-03-07 16:28:56

+0

不错的一个!应该管用 :) – masnun 2012-03-07 16:31:26

回答

1

有一个在Zend框架没有直接的exec()支持的一种方式。最接近命令行支持的是Zend_Console类,但它的意思是从命令行获取参数。

我会将exec()函数作为一个过程对象进行包装并使用它。这里有一个很好的例子从PHP文档:

<?php 
    // You may use status(), start(), and stop(). notice that start() method gets called automatically one time. 
    $process = new Process('ls -al'); 

    // or if you got the pid, however here only the status() metod will work. 
    $process = new Process(); 
    $process.setPid(my_pid); 
?> 

<?php 
    // Then you can start/stop/ check status of the job. 
    $process.stop(); 
    $process.start(); 
    if ($process.status()){ 
     echo "The process is currently running"; 
    }else{ 
     echo "The process is not running."; 
    } 
?> 

<?php 
/* An easy way to keep in track of external processes. 
* Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it. 
* @compability: Linux only. (Windows does not work). 
* @author: Peec 
*/ 
class Process{ 
    private $pid; 
    private $command; 

    public function __construct($cl=false){ 
     if ($cl != false){ 
      $this->command = $cl; 
      $this->runCom(); 
     } 
    } 
    private function runCom(){ 
     $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!'; 
     exec($command ,$op); 
     $this->pid = (int)$op[0]; 
    } 

    public function setPid($pid){ 
     $this->pid = $pid; 
    } 

    public function getPid(){ 
     return $this->pid; 
    } 

    public function status(){ 
     $command = 'ps -p '.$this->pid; 
     exec($command,$op); 
     if (!isset($op[1]))return false; 
     else return true; 
    } 

    public function start(){ 
     if ($this->command != '')$this->runCom(); 
     else return true; 
    } 

    public function stop(){ 
     $command = 'kill '.$this->pid; 
     exec($command); 
     if ($this->status() == false)return true; 
     else return false; 
    } 
} 
?> 

它也可以让你停下来检查一个工作的状态。