If your new page is saved to a DB using Eloquent, you can register an Eloquent Event, and call the Artisan command when the Eloquent event is fired.
Can routes be reloaded/refreshed from a controller?
I built a CMS using Laravel 5.2. I have a Pages module in my CMS where pages can be created, updated, and deleted. The page data is stored in the database.
When a page is created, I add a route to a custom route file that gets included at the bottom of my main routes.php file. I did this so that the database wouldn't need to be queried just to figure out what page to display. Here is an example of a route for a page created by this module ...
Route::get('/about-us', ['as' => 'page-1', function(){ return App::make('\App\Http\Controllers\PageController')->callAction('view', [1]); }]);
I am now trying to work in the generation of a sitemap. I created an Artisan command that does the work to actually generate the sitemap. That includes the static pages, the dynamic pages, and the blog posts and is working perfectly.
I was hoping to call this Artisan command when a new page has been created or an existing page has been updated (in order to update the lastmod field in the sitemap). This is where I have run into an issue. The route is written to my custom file and then I call the Artisan command ... on the same page load. Therefore, the new route is not included in the routes that the app is aware of.
Is there a method that I can call from within my controller to make the application reload the routes (and thus include the newly created route) before I make the call to my Artisan command? Or do I need to set up some sort of middleware to handle this? Any help/direction would be greatly appreciated. :)
My middleware approach is working like a charm.
I added Eloquent events to my AppServiceProvider for the models that I need to watch (Pages and Posts). If either is saved or deleted, it sets a session flash variable that indicates the sitemap needs to be updated.
My middleware checks for that session flash variable, and if it is set, it calls the Artisan command to update the sitemap.
I kinda wish I had thought of this solution from the start. :D
Please or to participate in this conversation.