Explanation
@davidsi02: since you are mentioning only one column, I guess you only need the URI.
I would create a command for this. The idea is to get the available routes in JSON format then insert them in the database.
Luckily, there is a command to get the available routes in a JSON format.
If you only want the URIs, the command would be:
php artisan route:list --columns=URI --json
You will get an array of json objects, each having a uri. Something like:
[{"uri":"users\/{user}"},{"uri":"posts"}]
Then, you will have to loop through it and store each uri in a database table.
Step by step solution
If you don't have it, create a database table called routes with a string column called uri
Then to generate the new command, run:
php artisan make:command RouteStoreCommand
Then replace the content of RouteStoreCommand.php with the following content:
<?php
namespace App\Console\Commands;
use DB;
use Illuminate\Console\Command;
use Symfony\Component\Console\Output\BufferedOutput;
class RouteStoreCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:store';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Store routes in the database';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->info('Fetching routes...');
// Output used to get the result from the route:list command
$output = new BufferedOutput();
$this->runCommand('route:list', [
'--json' => 1, '--columns' => ['URI']
], $output);
$routes = json_decode($output->fetch());
$this->info('Routes fetched !');
$this->info('Deleting existing routes...');
DB::table('routes')->delete();
$this->info('Routes deleted !');
$this->info('Storing routes...');
foreach ($routes as $route) {
DB::table('routes')->insert(['uri' => $route->uri]);
}
$this->info('Routes stored !');
}
}
Then you can execute the command by running:
php artisan route:store
Each time, you want to refresh the routes in the database, you can run this same custom command.
Please note that in this example, I delete all DB values each time the command is run. Indeed, that's the simplest way to avoid having duplication.