How do I list all unstable controllers in Laravel?

Asked

Viewed 357 times

0

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

There is also a controller called Controller, which is abstract, i.e., is only used to be extended by other controllers. In some cases, I also create my own Abstract Controller to 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 would like to somehow list these controllers (except those that are not instantiable), and save them in one array.

How could I do that?

I need them to stay like this:

[ 
  'Webservice/ServerController', 
  'Webservice/ClienteController', 
  'PageController',
  'UsuariosController'
]

1 answer

1


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

  • 1

    Thanks, João, for the contribution. I thought I would have to answer this, but finally I found an Artisan here at Stackoverlow +1

  • @Wallacemaxters, for nothing!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.