Latest Post

Tăng thứ hạng và truy cập tự nhiên với 10 phương pháp SEO hay nhất Kiếm Tiền Online (mmo): Khái Niệm và Các Hình Thức Phổ Biến

Artisan Command trong Laravel là một trong những cấu trúc quan trọng giúp xây dựng thương hiệu mạnh mẽ của mã nguồn mở Laravel. Artisan Command cung cấp một loạt các công cụ hữu ích cho các nhà phát triển, giúp họ xây dựng mã nguồn dễ dàng, thao tác với cơ sở dữ liệu bằng các chức năng như migrate, seed, kiểm tra thông tin về ứng dụng Laravel, và nhiều chức năng khác. Với sự hỗ trợ của Artisan Command, chúng ta sẽ khám phá thêm về tính năng mạnh mẽ này của Laravel tại Blog.

Laravel Artisan Comand là gì?

Artisan là một phần mềm dòng lệnh có sẵn kèm theo Laravel, một framework phát triển ứng dụng web. Artisan thường nằm trong thư mục gốc của ứng dụng Laravel và chứa một loạt các lệnh dòng lệnh hữu ích. Các lệnh Artisan giúp bạn tạo, quản lý và triển khai ứng dụng của mình một cách thuận tiện và hiệu quả.

Danh sách các lệnh Artisan Command

Một số command hữu ích mà bạn nên nhớ.

Liêt kê danh sách các command:

php artisan list

Kiểm tra version của Laravel

php artisan --version

Xem hướng dẫn của 1 tham số

php artisan help migrate

Khởi tạo server cho Laravel

php artisan serve

Danh sách 21 command 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 ta sử dụng lệnh artisan để tạo một command

php artisan make:command UploadFile

Trong ví dụ trên mình tạo 1 command với mục đích để upload file. Sau khi chạy lệnh nó sẽ tạo ra 1 file có tên là UploadFile.php ở thư mục app/Console/Commands/

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 lại để nó có thể chạy được như chúng ta mong 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 class UploadFile chúng ta có:

– Thuộc tính $signature là tên hoặc là ký hiệu của command.

protected $signature = 'upload_file';

– Thuộc tính $description là phần miêu tả ý nghĩa của command

protected $description = 'Example Upload file to server';

– Phương thức handle là hàm xử lý nội dùng cần thực hiện.

public function handle()
{
    print ("Start upload file to server vinasupport.com");
    // Upload Process here
}

Cuối cùng để chạy command trên chúng ta chạy thông qua artisan như sau:

php artisan upload_file

Kết quả:

Các tham số của Artisan Command

Để truyền các tham số vào Artisan Command chúng ta có các loại sau:

Option bắt buộc

protected $signature = 'order:check {param}';

Option tuỳ chọn

protected $signature = 'order:check {param?}';

Option với dữ liệu mặc định

protected $signature = 'order:check {param=foo}';

Để lấy giá trị Option truyền vào thì trong phương thức handle() chúng ta gọi ra:

$param = $this->option('param');

Để lấy all giá trị Options

$options = $this->options();

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *