Create a Artisan Console, writing a Command. It’s simple to make this controller value generation.
Place this Command in your Laravel application inside the folder app\Commands
:
<?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);
}
}
To make it work on php artisan
file app\Console\Kernel.php
and add one more line to the variable $commands
'App Commands Listcontrollers':
protected $commands = [
'App\Console\Commands\Inspire',
'App\Commands\ListControllers',
];
After adding and setting, go to the command prompt or correlative and do:
php artisan controller:list
On the screen will show an output of all instantiative controllers and in the same local folder of Laravel a file with the name of php files. who has the layout in array
of Controllers
Thanks, João, for the contribution. I thought I would have to answer this, but finally I found an Artisan here at Stackoverlow +1
– Wallace Maxters
@Wallacemaxters, for nothing!
– user46523