Console Command
Create a console command by running the "artisan make:command" in your terminal.
php artisan make:command GenerateSitemap
The content of the console command class should be as follows. Anytime the you run "site:generate-sitemap" command, the generate sitemap jobs will be dispatched and the sitemap will be generated.
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class GenerateSitemap extends Command { protected $signature = 'site:generate-sitemap'; protected $description = 'Generate sitemap file'; public function __construct() { parent::__construct(); } public function handle() { $this->info('Generating sitemap'); \App\Jobs\GenerateSitemap::dispatch(); return 0; } }
Route File
To call and execute it from the routes file you can use the "Artisan" facade.
# /routes/web.php use Illuminate\Support\Facades\Artisan; Route::get('/generate-sitemap, function () { Artisan::call("site:generate-sitemap"); });
When you define the artisan command like the code above, it will be equivalent to writing "php artisan site:generate-sitemap" on the terminal.
php artisan site:generate-sitemap
So now when you visit "/generate-sitemap" path, the console command will be run and the sitemap will be generated.
Related Tutorial
Leave a reply