Laravel 未定义函数

Laravel undefined function

我有以下功能:

# Get Directories of Path as Array
function dirToArray($dir) {

   $result = array();

   $cdir = scandir($dir);
   foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
         {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
         }
         else
         {
            $result[] = $value;
         }
      }
   }

   return $result;
}

并这样称呼它:

        $watchFolder = 'C:\xampp\htdocs\project\One\';
        $this->dirToArray($watchFolder);

但我收到以下错误:

Call to undefined function App\Http\Controllers\dirToArray()

有人知道怎么解决吗?

编辑: 所以我的完整控制器看起来像这样:

<?php

namespace App\Http\Controllers;

use DB;
use Mail;
use File;
use Input;
use Request;
use Illuminate\Filesystem\Filesystem;
use App\Http\Controllers\Controller;

class ProductRController extends Controller
{

    public function createProduct()
    {
            ...
            $watchFolder = 'C:\xampp\htdocs\Product\R\';
            $this->dirToArray($watchFolder);

            return redirect()->route('root')->with('message', 'Success')->withInput();
        }
        else
        {
            return redirect()->route('newProduct')->with('message', 'Error')->withInput();
        }

    }

    # Get Directories of Path as Array
    function dirToArray($dir) {

       $result = array();

       $cdir = scandir($dir);
       foreach ($cdir as $key => $value)
       {
          if (!in_array($value,array(".","..")))
          {
             if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
             {
                $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
             }
             else
             {
                $result[] = $value;
             }
          }
       }

       return $result;
    }

}

您仍然需要像在函数外那样在函数内使用 $this->,因为 $this 代表您调用方法的对象:

$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);