Execute shell from controller CakePHP 3.x

前端 未结 3 847
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 01:31

I have a specific task in a CakePHP Shell and it\'s executed by a CRON job. But I want the users to be able to execute it from a web interface (like a button or something like t

3条回答
  •  清歌不尽
    2021-01-24 02:03

    in order to call shell from your controller function you need to do this in your controller function :

    namespace App\Controller;
    
    use App\Controller\AppController;
    use Cake\Console\ShellDispatcher; //use this shell dispacher
    
    /**
    * Example Controller
    *
    */
    class ExampleController extends AppController
    {
    
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $shell = new ShellDispatcher(); //create object of your Shell Dispatcher
        $output = $shell->run(['cake', 'foo']); //here foo is your shell name
    
        if (0 === $output) {
            $this->Flash->success('Success from shell command.');
        } else {
            $this->Flash->error('Failure from shell command.');
        }
    
        return $this->redirect('/');
      }
    }
    

    hope this answer your question, if any problem leave a comment.

    thank you.

提交回复
热议问题