我有以下功能:Laravel未定义功能
# 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;
}
}
你在哪里把你的功能? –
我把它放在一个控制器 –
另外你在哪里把它的内部?它也不会在工作/不工作水平关系在这里但它是一个最佳实践(和PHP标准)显式声明的方法为'public' –