├── README.md ├── composer.json └── src ├── RoutesCommand.php └── RoutesCommandServiceProvider.php /README.md: -------------------------------------------------------------------------------- 1 | # Lumen Route List Display 2 | 3 | [![Total Downloads](https://poser.pugx.org/appzcoder/lumen-routes-list/d/total.svg)](https://packagist.org/packages/appzcoder/lumen-routes-list) 4 | [![Latest Stable Version](https://poser.pugx.org/appzcoder/lumen-routes-list/v/stable.svg)](https://packagist.org/packages/appzcoder/lumen-routes-list) 5 | [![Latest Unstable Version](https://poser.pugx.org/appzcoder/lumen-routes-list/v/unstable.svg)](https://packagist.org/packages/appzcoder/lumen-routes-list) 6 | [![License](https://poser.pugx.org/appzcoder/lumen-routes-list/license.svg)](https://packagist.org/packages/appzcoder/lumen-routes-list) 7 | 8 | 9 | ## Installation 10 | 11 | 1. Run 12 | ``` 13 | composer require appzcoder/lumen-routes-list 14 | ``` 15 | 16 | 2. Add service provider into **/bootstrap/app.php** file. 17 | ```php 18 | $app->register(Appzcoder\LumenRoutesList\RoutesCommandServiceProvider::class); 19 | ``` 20 | 3. Run ```composer dump-autoload``` 21 | 22 | ## Commands 23 | 24 | ``` 25 | php artisan route:list 26 | ``` 27 | 28 | 29 | ## Author 30 | 31 | Sohel Amin 32 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appzcoder/lumen-routes-list", 3 | "license": "MIT", 4 | "description": "Display the route list of Lumen", 5 | "keywords": [ 6 | "lumen", 7 | "route list" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "Sohel Amin", 12 | "email": "sohelamincse@gmail.com", 13 | "homepage": "http://www.appzcoder.com" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.5.9|^7.0|^8.0", 18 | "illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Appzcoder\\LumenRoutesList\\": "src/" 23 | } 24 | }, 25 | "minimum-stability": "stable", 26 | "prefer-stable": true 27 | } 28 | -------------------------------------------------------------------------------- /src/RoutesCommand.php: -------------------------------------------------------------------------------- 1 | displayRoutes($this->getRoutes()); 48 | } 49 | 50 | /** 51 | * Compile the routes into a displayable format. 52 | * 53 | * @return array 54 | */ 55 | protected function getRoutes() 56 | { 57 | global $app; 58 | 59 | $routeCollection = property_exists($app, 'router') ? $app->router->getRoutes() : $app->getRoutes(); 60 | $rows = array(); 61 | foreach ($routeCollection as $route) { 62 | $controller = $this->getController($route['action']); 63 | // Show class name without namesapce 64 | if ($this->option('compact') && $controller !== 'None') 65 | $controller = substr($controller, strrpos($controller, '\\') + 1); 66 | 67 | $rows[] = [ 68 | 'verb' => $route['method'], 69 | 'path' => $route['uri'], 70 | 'namedRoute' => $this->getNamedRoute($route['action']), 71 | 'controller' => $controller, 72 | 'action' => $this->getAction($route['action']), 73 | 'middleware' => $this->getMiddleware($route['action']), 74 | ]; 75 | } 76 | 77 | return $this->pluckColumns($rows); 78 | } 79 | 80 | /** 81 | * @param array $action 82 | * @return string 83 | */ 84 | protected function getNamedRoute(array $action) 85 | { 86 | return (!isset($action['as'])) ? "" : $action['as']; 87 | } 88 | 89 | /** 90 | * @param array $action 91 | * @return mixed|string 92 | */ 93 | protected function getController(array $action) 94 | { 95 | if (empty($action['uses'])) { 96 | return 'None'; 97 | } 98 | 99 | return current(explode("@", $action['uses'])); 100 | } 101 | 102 | /** 103 | * @param array $action 104 | * @return string 105 | */ 106 | protected function getAction(array $action) 107 | { 108 | if (!empty($action['uses'])) { 109 | $data = $action['uses']; 110 | if (($pos = strpos($data, "@")) !== false) { 111 | return substr($data, $pos + 1); 112 | } else { 113 | return "METHOD NOT FOUND"; 114 | } 115 | } else { 116 | return 'Closure'; 117 | } 118 | } 119 | 120 | /** 121 | * @param array $action 122 | * @return string 123 | */ 124 | protected function getMiddleware(array $action) 125 | { 126 | return (isset($action['middleware'])) 127 | ? (is_array($action['middleware'])) 128 | ? join(", ", $action['middleware']) 129 | : $action['middleware'] : ''; 130 | } 131 | 132 | /** 133 | * Remove unnecessary columns from the routes. 134 | * 135 | * @param array $routes 136 | * @return array 137 | */ 138 | protected function pluckColumns(array $routes) 139 | { 140 | return array_map(function ($route) { 141 | return Arr::only($route, $this->getColumns()); 142 | }, $routes); 143 | } 144 | 145 | /** 146 | * Display the route information on the console. 147 | * 148 | * @param array $routes 149 | * @return void 150 | */ 151 | protected function displayRoutes(array $routes) 152 | { 153 | if (empty($routes)) { 154 | return $this->error("Your application doesn't have any routes."); 155 | } 156 | 157 | $this->table($this->getHeaders(), $routes); 158 | } 159 | 160 | /** 161 | * Get the table headers for the visible columns. 162 | * 163 | * @return array 164 | */ 165 | protected function getHeaders() 166 | { 167 | return Arr::only($this->headers, array_keys($this->getColumns())); 168 | } 169 | 170 | /** 171 | * Get the column names to show (lowercase table headers). 172 | * 173 | * @return array 174 | */ 175 | protected function getColumns() 176 | { 177 | $availableColumns = array_map('lcfirst', $this->headers); 178 | 179 | if ($this->option('compact')) { 180 | return array_intersect($availableColumns, $this->compactColumns); 181 | } 182 | 183 | if ($columns = $this->option('columns')) { 184 | return array_intersect($availableColumns, array_map('lcfirst', $columns)); 185 | } 186 | 187 | return $availableColumns; 188 | } 189 | 190 | /** 191 | * Get the console command options. 192 | * 193 | * @return array 194 | */ 195 | protected function getOptions() 196 | { 197 | return [ 198 | [ 199 | 'columns', 200 | null, 201 | InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 202 | 'Columns to include in the route table (' . implode(', ', $this->headers) . ')' 203 | ], 204 | 205 | [ 206 | 'compact', 207 | 'c', 208 | InputOption::VALUE_NONE, 209 | 'Only show verb, path, controller and action columns' 210 | ] 211 | ]; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/RoutesCommandServiceProvider.php: -------------------------------------------------------------------------------- 1 | commands('Appzcoder\LumenRoutesList\RoutesCommand'); 17 | } 18 | } 19 | --------------------------------------------------------------------------------