Create a Artisan Console by typing an Command . It is simple to do this generation of Controllers values.
Place this Command in your Laravel application, inside the app\Commands
folder:
<?php namespace App\Commands;
use Illuminate\Console\Command;
class ListControllers extends Command {
protected $name = 'controllers:list';
protected $description = 'List of Controllers';
protected $signature = 'controllers:list';
public function handle()
{
$this->comment('');
$this->comment(' List Controllers');
$this->comment('*------------------------------------------------------*');
$this->comment('');
$array = $this->listControllers();
$this->save($array);
$this->comment('');
$this->comment('*------------------------------------------------------*');
}
protected function listControllers($view = true)
{
$path = app_path('Http/Controllers');
$namespaceControllers = '\App\Http\Controllers';
$dir = new \RecursiveDirectoryIterator($path);
foreach (new \RecursiveIteratorIterator($dir) as $filename => $file) {
if ($file->isFile() && $filename != '.' && $filename != '..')
{
$filename = str_replace($path, '', $filename);
if ((new \ReflectionClass($namespaceControllers.str_replace('.php','', $filename)))->isInstantiable())
{
$array[] = substr(str_replace('.php', '', $filename), 1);
if ($view)
{
$this->comment(substr(str_replace('.php', '', $filename), 1));
}
}
}
}
return $array;
}
protected function save(array $array = array(), $path = "files.php")
{
if (file_exists($path))
{
unlink($path);
}
$fp = fopen($path, "a+");
fwrite($fp, "['");
fwrite($fp, implode("','",$array));
fwrite($fp, "']");
fclose($fp);
}
}
In order to work in php artisan
enter in the file app\Console\Kernel.php
and add another line in the variable $commands
'App \ Commands \ ListControllers' p>
protected $commands = [
'App\Console\Commands\Inspire',
'App\Commands\ListControllers',
];
After adding and configuring, go to the command prompt or correlative and do:
php artisan controller:list
On the screen will show an output of all the instantiable controllers and in the same local folder of Laravel a file named files.php that has layout in array
of Controllers