Tạo Artisan Command trong Laravel
Một trong những cấu trúc quan trọng tạo nên thương hiệu của mã nguồn mở Laravel là Artisan Command. Với Artisan Command, nó hỗ trợ bạn xây dựng mã nguồn cơ bản, thao tác với cơ sở dữ liệu như di chuyển, tạo hạt giống, kiểm tra thông tin Laravel và nhiều chức năng khác. Bây giờ hãy cùng vinasupport.com tìm hiểu về Laravel Artisan Command nhé!
Laravel Artisan Comand là gì?
Artisan là một Command Interface đi kèm với Laravel. Artisan tồn tại ở gốc ứng dụng của bạn dưới dạng tập lệnh thủ công và cung cấp một số lệnh hữu ích có thể hỗ trợ bạn trong khi xây dựng ứng dụng của mình.
Danh sách các lệnh thủ công
Một số lệnh hữu ích mà bạn nên nhớ.
Danh sách các lệnh:
php artisan list
Kiểm tra phiên bản Laravel
php artisan --version
Xem hướng dẫn của 1 thông số
php artisan help migrate
Khởi tạo máy chủ cho Laravel
php artisan serve
Danh sách 21 lệnh của Artisan Command
make:channel Create a new channel class make:command Create a new Artisan command make:controller Create a new controller class make:event Create a new event class make:exception Create a new custom exception class make:factory Create a new model factory make:job Create a new job class make:listener Create a new event listener class make:mail Create a new email class make:middleware Create a new middleware class make:migration Create a new migration file make:model Create a new Eloquent model class make:notification Create a new notification class make:observer Create a new observer class make:policy Create a new policy class make:provider Create a new service provider class make:request Create a new form request class make:resource Create a new resource make:rule Create a new validation rule make:seeder Create a new seeder class make:test Create a new test class
Tạo Laravel Artisan Command
Chúng tôi sử dụng lệnh artisan để tạo lệnh
php artisan make:command UploadFile
Trong ví dụ trên, tôi đã tạo một lệnh với mục đích tải File lên. Sau khi chạy lệnh, nó sẽ tạo một File có tên UploadFile.php trong thư mục ứng dụng/Control Panel/Lệnh/
Với nội dung mặc định như sau:
<?php namespace AppConsoleCommands; use IlluminateConsoleCommand; class UploadFile extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature="command:name"; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Execute the console command. * * @return int */ public function handle() { return 0; } }
Bây giờ chúng ta sẽ sửa nó để nó có thể chạy theo ý muốn như sau:
<?php namespace AppConsoleCommands; use IlluminateConsoleCommand; class UploadFile extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature="upload_file"; /** * The console command description. * * @var string */ protected $description = 'Example Upload file to server'; /** * Execute the console command. * */ public function handle() { print ("Start upload file to server vinasupport.com"); // Upload Process here } }
Giải thích ý nghĩa của lớp UploadFile ta có:
– Của cải chữ ký $ là tên hoặc ký hiệu của lệnh.
protected $signature="upload_file";
– Của cải $mô tả là phần mô tả ý nghĩa của mệnh lệnh
protected $description = 'Example Upload file to server';
– Cách xử lý là chức năng xử lý nội bộ được thực hiện.
public function handle() { print ("Start upload file to server vinasupport.com"); // Upload Process here }
Cuối cùng để chạy lệnh trên ta chạy qua artisan như sau:
php artisan upload_file
Kết quả:
Các thông số của Artisan Command
Để truyền tham số cho Artisan Command ta có các kiểu sau:
Tùy chọn bắt buộc
protected $signature="order:check {param}";
tùy chọn tùy chọn
protected $signature="order:check {param?}";
Tùy chọn với dữ liệu mặc định
protected $signature="order:check {param=foo}";
Để lấy giá trị Option được truyền vào, trong phương thức handle() chúng ta gọi:
$param = $this->option('param');
Để có được tất cả các Tùy chọn. giá trị
$options = $this->options();
Nguồn: vinasupport.com