Laravel 9 Call Artisan Command From Closure Routes

Reading Time: 3 minutes
1,069 Views

Inside this article we will see the concept i.e Laravel 9 Call Artisan Command From Closure Routes Tutorial. Article contains the classified information about How To use artisan commands From Closure Routes.

If you are looking for a solution i.e how to execute artisan command from application routes in laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

This article is not version specific, you can use use it with Laravel v6, v7, v8, v9.

Read More: Laravel 9 Call Artisan Command From Controller Tutorial

Closures are anonymous functions that don’t belong to any class or object. Closures don’t have specified names.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Route

Open web.php file from /routes folder. Add these routes into it.

//...

Route::get("clear-cache", function () {
    /* php artisan cache:clear */
    \Artisan::call('cache:clear');

    dd('Application cache cleared successfully.');
});

Route::get("clear-views", function () {
    /* php artisan view:clear */
    \Artisan::call('view:clear');

    dd('Compiled views cleared successfully.');
});

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

To clear application’s cache

URL: http://127.0.0.1:8000/clear-cache

Read More: Laravel 9 How To Redirect with Flashdata Example Tutorial

To clear application’s compiled views

URL: http://127.0.0.1:8000/clear-views

We hope this article helped you to learn Laravel 9 Call Artisan Command From Closure Routes in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.