1.Call Artisan Command from Command Line
You can call your artisan command from command line like :
You can call your artisan command from command line like :
php artisan cache:clear
- Call Artisan Command from Controller
- routes.php
Add this get method in your routing file.
Route::get(‘test’, ‘TestController@test’); Now Create a controller file name as “TestController.php” under “app\Http\Controllers”.
<?php
namespace
App\Http\Controllers;
use
App\Http\Controllers\Controller;
use
Artisan;
class
TestController
extends
Controller {
public
function
test(){
Artisan::call(
'cache:clear'
);
}
}
?>
You can call your artisan command from route file as well.
routes.php
Route::get(‘/my_artisan’, function () {Artisan::call(“cache:clear”););});
Now you can execute the above route from your browser like /my_artisan
Now we will discuss all Artisan command one by one. First of all, to see what commands are available in laravel we will execute below command:
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"list"
);dd(Artisan::output());});
Usage:\n
command [options] [arguments]\n
\n
Options:\n
-h, --help Display this help message\n
-q, --quiet Do not output any message\n
-V, --version Display this application version\n
--ansi Force ANSI output\n
--no-ansi Disable ANSI output\n
-n, --no-interaction Do not ask any interactive question\n
--env[=ENV] The environment the command should run under.\n
-v|vv|vvv, --verbose Increase the verbosity of messages: 1
for
normal output, 2
for
more verbose output
and
3
for
debug\n
\n
Available commands:\n
clear-compiled Remove the compiled
class
file\n
down Put the application into maintenance mode\n
env Display the current framework environment\n
help Displays help
for
a command\n
inspire Display an inspiring quote\n
list Lists commands\n
migrate Run the database migrations\n
optimize Optimize the framework
for
better performance\n
serve Serve the application on the PHP development server\n
tinker Interact with your application\n
up Bring the application out of maintenance mode\n
app\n
app:name Set the application
namespace
\n
auth\n
auth:clear-resets
Flush
expired password reset tokens\n
cache\n
cache:clear
Flush
the application cache\n
cache:table Create a migration
for
the cache database table\n
config\n
config:cache Create a cache file
for
faster configuration loading\n
config:clear Remove the configuration cache file\n
db\n
db:seed Seed the database with records\n
event\n
event:generate Generate the missing events
and
listeners based on registration\n
handler\n
handler:command Create a
new
command handler
class
\n
handler:event Create a
new
event handler
class
\n
key\n
key:generate Set the application key\n
make\n
make:command Create a
new
command
class
\n
make:console Create a
new
Artisan command\n
make:controller Create a
new
resource controller
class
\n
make:event Create a
new
event
class
\n
make:job Create a
new
job
class
\n
make:listener Create a
new
event listener
class
\n
make:middleware Create a
new
middleware
class
\n
make:migration Create a
new
migration file\n
make:model Create a
new
Eloquent model
class
\n
make:provider Create a
new
service provider
class
\n
make:request Create a
new
form request
class
\n
make:seeder Create a
new
seeder
class
\n
migrate\n
migrate:install Create the migration repository\n
migrate:refresh Reset
and
re-run all migrations\n
migrate:reset Rollback all database migrations\n
migrate:rollback Rollback the last database migration\n
migrate:status Show the status of each migration\n
queue\n
queue:failed List all of the failed queue jobs\n
queue:failed-table Create a migration
for
the failed queue jobs database table\n
queue:
flush
Flush
all of the failed queue jobs\n
queue:forget
Delete
a failed queue job\n
queue:listen Listen to a given queue\n
queue:restart Restart queue worker daemons after their current job\n
queue:retry Retry a failed queue job\n
queue:subscribe Subscribe a URL to an Iron.io push queue\n
queue:table Create a migration
for
the queue jobs database table\n
queue:work Process the next job on a queue\n
route\n
route:cache Create a route cache file
for
faster route registration\n
route:clear Remove the route cache file\n
route:list List all registered routes\n
schedule\n
schedule:run Run the scheduled commands\n
session\n
session:table Create a migration
for
the session database table\n
vendor\n
vendor:publish Publish any publishable assets from vendor packages\n
view\n
view:clear Clear all compiled view files\n
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'clear-compiled'
);});
clear-compiled
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'down'
);});
env
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'env'
);});
help
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'help'
);dd(Artisan::output())});
Usage:\n
help [options] [--] [<command_name>]\n
\n
Arguments:\n
command The command to execute\n
command_name The command name [
default
:
"help"
]\n
\n
Options:\n
--xml To output help
as
XML\n
--format=FORMAT The output format (txt, xml, json,
or
md) [
default
:
"txt"
]\n
--raw To output raw command help\n
-h, --help Display this help message\n
-q, --quiet Do not output any message\n
-V, --version Display this application version\n
--ansi Force ANSI output\n
--no-ansi Disable ANSI output\n
-n, --no-interaction Do not ask any interactive question\n
--env[=ENV] The environment the command should run under.\n
-v|vv|vvv, --verbose Increase the verbosity of messages: 1
for
normal output, 2
for
more verbose output
and
3
for
debug\n
\n
Help:\n
The help command displays help
for
a given command:\n
\n
php /cs/
public
/index.php help list\n
\n
You can also output the help in other formats by using the --format option:\n
\n
php /cs/
public
/index.php help --format=xml list\n
\n
To display the list of available commands, please
use
the list command.\n
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'inspire'
);dd(Artisan::output())});
list
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'list'
);dd(Artisan::output())});
optimize
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'optimize'
,[
'--force'
=>
'force'
]);});
serve
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"serve"
,[
"--host"
=>
'127.0.0.1'
]);});
up
You can’t use this function from controller or route. You must execute from command line to make it live.
php artisan up
app:name
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"app:name"
,[
'name'
=>
'TestApp'
]));});
auth:clear-resets
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"auth:clear-resets"
));});
cache:clear
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"cache:clear"
);});
cache:clear
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"cache:table"
));});
The filename is create according to current date and time like this format: YYYY_mm_dd_His_create_cache_table.php
config:cache
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"config:cache"
));});
config:clear
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"config:clear"
));});
event:generate
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"event:generate"
);});
protected
$listen
= [
'App\Events\SomeEvent'
=> [
'App\Listeners\EventListener'
,
],
];
handler:command
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"handler:command"
,[
'name'
=>
'TestCommand'
]);});
handler:event
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"handler:event"
,[
'name'
=>
'TestEvent'
]);});
key:generate
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"key:generate"
));});
make:command
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:command"
,[
'name'
=>
'TestCommand'
]));});
make:console
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:console"
,[
'name'
=>
'TestConsole'
]));});
make:controller
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:controller"
,[
'name'
=>
'TestController'
]));});
make:event
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:event"
,[
'name'
=>
'TestEvent'
]));});
make:job
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:job"
,[
'name'
=>
'TestJob'
]));});
make:listener
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:listener"
,[
'name'
=>
'TestListener'
]));});
make:middleware
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:middleware"
,[
'name'
=>
'TestMiddleware'
]));});
make:migration
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:migration"
,[
'name'
=>
'TestMigration'
]));});
make:model
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:model"
,[
'name'
=>
'TestModel'
]));});
make:provider
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:provider"
,[
'name'
=>
'TestProviders'
]));});
make:request
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:request"
,[
'name'
=>
'TestRequests'
]));});
make:seeder
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"make:seeder"
,[
'name'
=>
'TestSeeder'
]));});
How to use migrate command?
migrate
Route::get('/my_artisan,
function
() { (Artisan::call(
"migrate"
));});
migrate:install
Route::get('/my_artisan,
function
() { (Artisan::call(
"migrate:install"
));});
migrate:refresh
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"migrate:refresh"
));});
migrate:reset
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"migrate:reset"
));});
migrate:rollback
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"migrate:rollback"
));});
migrate:status
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"migrate:status"
);dd(Artisan::output());});
+------+------------------------------------------------+\n
| Ran? | Migration |\n
+------+------------------------------------------------+\n
| N | 2014_10_12_000000_create_users_table |\n
| N | 2014_10_12_100000_create_password_resets_table |\n
| N | 2016_04_02_131806_create_jobs_table |\n
| N | 2016_06_10_112609_create_cache_table |\n
+------+------------------------------------------------+\n
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'queue:failed'
);});
+----+------------+---------+---------------------+-----------+\n
| ID | Connection | Queue | Class | Failed At |\n
+----+------------+---------+---------------------+-----------+\n
| 1 | Test | Testing | 2016-06-21 13:19:37 | |\n
+----+------------+---------+---------------------+-----------+\n
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'queue:failed-table'
);});
queue:flush
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'queue:flush'
);});
queue:forget
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"queue:forget"
,[
'id'
=>
'2'
]));});
queue:restart
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'queue:restart'
);});
queue:table
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
'queue:table'
);});
How to use Route Artisan Command
route:cache
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"route:cache"
);});
Unable to prepare route [/] for serialization. Uses Closure.
It means you can use Closure only in your route file.
You can call this artisan from from either controller’s method or from command line:
Controller : Artisan::call(
"route:cache"
);
CLI : php artisan route:cache
route:clear
Route::get(
'/my_artisan'
,
function
() { (Artisan::call(
"route:clear"
));});
route:clear
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"route:list"
);dd(Artisan::output());});
+--------+----------+------------+------+--------------------------------------------------+------------+\n
| Domain | Method | URI | Name | Action | Middleware |\n
+--------+----------+------------+------+--------------------------------------------------+------------+\n
| | GET|HEAD | / | | Closure | |\n
| | GET|HEAD | foo | | Closure | |\n
| | GET|HEAD | register | | App\Http\Controllers\UserController@register | |\n
| | POST | save_user | | App\Http\Controllers\UserController@SaveRegister | |\n
| | GET|HEAD | users/{id} | | Closure | |\n
+--------+----------+------------+------+--------------------------------------------------+------------+\n
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"schedule:run"
);});
session:table
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"session:table"
);});
view:clear
Route::get(
'/my_artisan'
,
function
() {Artisan::call(
"view:clear"
);});
Keep in touch for more tutorials.
No comments:
Post a Comment