How do I list all instanciable controllers in Laravel?

0

In Laravel 5, we have a folder called Http\Controllers which has the controllers used in the application.

There is also a controller called Controller , which is abstract, that is, it is used only to be extended by other controllers. In some cases, I also usually create my own Abstract Controller, so I can extend functionality in other Controllers.

Example:

  app/
    Http
       /Controllers
          Controller.php #abstrato
          Webservice/
               AbstractController.php #abstrato
               ServerController.php
               ClienteController.php

          PageController.php
          UsuariosController.php

I'd like to list those controllers (except those that are not instantiable), and save them in a array .

How could I do this?

I need them to look like this:

[ 
  'Webservice/ServerController', 
  'Webservice/ClienteController', 
  'PageController',
  'UsuariosController'
]
    
asked by anonymous 18.05.2016 / 16:29

1 answer

1

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

    
24.05.2016 / 19:36