post-image

Laravel Artisan là gì, tại sao nói công cụ này cực hữu ích?

Tổng quan

Khi mới làm quen với framework Laravel, tôi cũng như bạn rất ngạc nhiên khi biết có một công cụ dòng lệnh. Tôi vốn xuất thân từ một quản trị mạng, nói đến các công cụ dòng lệnh (command line hay console) là nghĩ ngay đến các cấu hình thiết bị mạng, nhưng giờ đây khi làm lập trình thì Laravel Artisan là cái quái quỉ gì vậy, chẳng lẽ cũng dùng nó để cấu hình?

1. Laravel Artisan là cái gì?

Artisan phiên âm /ärdəzən/ có ý nghĩa là nghệ nhân, là người xử lý một công việc nào đó thủ công một cách cực xuất sắc.

Laravel Artisan là một công cụ dòng lệnh được tích hợp sẵn trong các dự án sử dụng Laravel, nó cung cấp rất nhiều các chức năng trợ giúp việc xây dự án, giảm thời gian viết code cũng như tự động hóa một số công việc. Laravel Artisan xứng đáng với cái tên của nó, nó thật sự xuất sắc khi xử lý các công việc mang tính thủ công bằng cách tự động hóa chúng.

  • Hỗ trợ các công việc liên quan đến vận hành dự án như tối ưu hóa, chuyển ứng dụng sang chế độ bảo trì, chạy các công việc ngầm theo kiểu hàng đợi (queue job), tạo và thay đổi sử dụng bộ đệm dữ liệu.
  • Các công việc xử lý cơ sở dữ liệu như migrate, đưa dữ liệu vào database, tạo dữ liệu kiểm thử.
  • Artisan cũng có thể tạo các template là các Class trong lập trình theo các mẫu khác nhau như tạo ra các Model, Controller, Event…
  • Các công việc liên quan đến bảo mật như cài đặt xác thực người dùng, sinh key mã hóa và các việc liên quan đến sử dụng OAuth2.
  • Chạy các công cụ ngoài như Tinker, hoặc cho phép người dùng tạo ra các công cụ tùy thích.

Bạn có thể sử dụng câu lệnh php artisan list để xem danh sách các câu lệnh hỗ trợ bởi Artisan:

c:\xampp\htdocs\allaravel>php artisan list Laravel Framework 5.4.19 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: clear-compiled Remove the compiled class file down Put the application into maintenance mode env Display the current framework environment help Displays help for a command inspire Display an inspiring quote list Lists commands migrate Run the database migrations optimize Optimize the framework for better performance serve Serve the application on the PHP development server tinker Interact with your application up Bring the application out of maintenance mode app app:name Set the application namespace auth auth:clear-resets Flush expired password reset tokens cache cache:clear Flush the application cache cache:forget Remove an item from the cache cache:table Create a migration for the cache database table config config:cache Create a cache file for faster configuration loading config:clear Remove the configuration cache file db db:seed Seed the database with records event event:generate Generate the missing events and listeners based on regist ration key key:generate Set the application key make make:auth Scaffold basic login and registration views and routes make:command Create a new Artisan command make:controller Create a new controller class make:event Create a new event class 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:policy Create a new policy class make:provider Create a new service provider class make:request Create a new form request class make:seeder Create a new seeder class make:test Create a new test class migrate migrate:install Create the migration repository migrate:refresh Reset and re-run all migrations migrate:reset Rollback all database migrations migrate:rollback Rollback the last database migration migrate:status Show the status of each migration notifications notifications:table Create a migration for the notifications table passport passport:client Create a client for issuing access tokens passport:install Run the commands necessary to prepare Passport for use passport:keys Create the encryption keys for API authentication queue queue:failed List all of the failed queue jobs queue:failed-table Create a migration for the failed queue jobs database tab le queue:flush Flush all of the failed queue jobs queue:forget Delete a failed queue job queue:listen Listen to a given queue queue:restart Restart queue worker daemons after their current job queue:retry Retry a failed queue job queue:table Create a migration for the queue jobs database table queue:work Start processing jobs on the queue as a daemon route route:cache Create a route cache file for faster route registration route:clear Remove the route cache file route:list List all registered routes schedule schedule:run Run the scheduled commands session session:table Create a migration for the session database table storage storage:link Create a symbolic link from "public/storage" to "storage/ app/public" vendor vendor:publish Publish any publishable assets from vendor packages view view:clear Clear all compiled view files

Đến đây chắc hẳn bạn cũng đã nắm được Laravel Artisan là cái gì và nó được sử dụng như thế nào? Tuy nhiên còn rất nhiều điều cần nói đến về Artisan, chúng ta tiếp tục nhé.

2. Laravel REPL

REPL là gì? REPL viết tắt của Read – Eval – Print Loop, còn được biết đến với tên language shell, đơn giản là một ngôn ngữ cho phép lặp đi lặp lại các việc Đọc dữ liệu, Tính toán dữ liệu và In ra màn hình. Các ứng dụng Laravel trong đó có Tinker là một ngôn ngữ REPL được hỗ trợ bởi PsySH.

PsySH: A runtime developer console, interactive debugger and REPL for PHP. Công cụ dòng lệnh runtime cho môi trường phát triển, tương tác khi debug và REPL trong ngôn ngữ PHP

Laravel Tinker cho phép bạn tương tác với các thực thể trong ứng dụng như Eloquent ORM, job, event… Nó cũng rất tiện cho chúng ta khi muốn kiểm tra một dòng lệnh xem nó hoạt động thế nào, trong rất nhiều bài viết tôi đã sử dụng Laravel Tinker như một công cụ để chạy các dòng lệnh mẫu thay vì phải viết hẳn một ví dụ mẫu. Để chạy Laravel Tinker bạn sử dụng câu lệnh php artisan tinker

c:\xampp\htdocs\allaravel>php artisan tinker Psy Shell v0.8.3 (PHP 5.6.20 ΓÇö cli) by Justin Hileman New version is available (current: v0.8.3, latest: v0.8.8) >>>
Code language: CSS (css)

Chúng ta cùng thực hiện thử một ví dụ với Laravel Tinker tìm hiểu về phương thức contains() trong xử lý các Laravel Collection:

>>> $frameworks = collect([ ['name' => 'Laravel', 'version' => '5.4'], ['name' => 'Symfony', 'version' => '3.2'], ['name' => 'CodeIgniter', 'version' => '3.1.4'], ['name' => 'Yii', 'version' => '2.0.11'] ]); => Illuminate\Support\Collection {#731 all: [ [ "name" => "Laravel", "version" => "5.4", ], [ "name" => "Symfony", "version" => "3.2", ], [ "name" => "CodeIgniter", "version" => "3.1.4", ], [ "name" => "Yii", "version" => "2.0.11", ], ], } >>> $frameworks->contains(['name' => 'Laravel', 'version' => '5.4']); => true >>> $frameworks->contains(['name' => 'PHPCake', 'version' => '3.1']); => false >>>
Code language: PHP (php)

3. Câu lệnh Laravel Artisan cho mục đích riêng

3.1 Tạo template cho câu lệnh Artisan

Bạn hoàn toàn có thể tạo ra các câu lệnh Artisan cho các mục đích riêng, các file class PHP cho các câu lệnh này thường được lưu vào thư mục app\Console\Commands. Artisan sẽ tạo ra một template sẵn cho câu lệnh cá nhân bằng lệnh php artisan make:command, nó sẽ tạo ra một file class cho câu lệnh này trong app\Console\Commands.

Trong ví dụ sau đây chúng ta sẽ tạo ra một câu lệnh mỗi khi thực hiện nó sẽ tạo ra một hoặc nhiều user thường hoặc user có quyền quản trị cho hệ thống. Đầu tiên tạo ra class UserCommand bằng artisan make:command.

c:\xampp\htdocs\allaravel>php artisan make:command UserCommand Console command created successfully.
Code language: CSS (css)

Khi đó bạn sẽ thấy file UserCommand.php trong app\Console\Commands với nội dung mẫu như sau:

<?php namespace App\Console\Commands; use Illuminate\Console\Command; class UserCommand 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'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // } }
Code language: HTML, XML (xml)

Lớp này là kế thừa lớp Command và có hai thuộc tính bạn cần để ý là 𝑠𝑖𝑔𝑛𝑎𝑡𝑢𝑟𝑒𝑣àsignaturevàdescription, nó sẽ được sử dụng để hiển thị thông tin khi in danh sách câu lệnh bằng php artisan list. Phương thức handle() sẽ được gọi đến khi thực hiện câu lệnh.

3.2 Định nghĩa các tham số và tùy chọn cho câu lệnh Artisan

Các tham số cho câu lệnh Artisan được định nghĩa trong thuộc tính $signature. Thuộc tính này cho phép bạn định nghĩa tên câu lệnh, tham số và các tùy chọn. Các tham số được định nghĩa trong hai dấu ngoặc nhọn {parameter}. Chúng ta cũng có thể mặc định giá trị ban đầu cho các tham số.

/** * The name and signature of the console command. * * @var string */ protected $signature = 'user:create {userType} {numberOfUser=1}';
Code language: PHP (php)

Khi đó, muốn tạo ra 10 user có quyền quản trị chúng ta sẽ thực hiện php artisan user:create admin 10

Các tùy chọn trong câu lệnh Artisan cũng được định nghĩa trong $signature, cũng được đưa vào hai dấu ngoặc nhọn với tiền tố là –, ví dụ {–options}, có hai loại tùy chọn là loại nhận giá trị và loại không.

/** * The name and signature of the console command. * * @var string */ protected $signature = 'user:create {userType} {numberOfUser} {--queue}';
Code language: PHP (php)

Trong ví dụ trên –queue sẽ được sẽ được xác định khi thực hiện câu lệnh artisan, khi đó giá trị này trong tùy chọn sẽ là true, ngược lại nếu không truyền –queue thì giá trị này trong tùy chọn sẽ là false. Giả sử câu lệnh php artisan user:create admin 10 –queue là sẽ tạo ra 10 quản trị viên bằng cách đưa công việc này vào hàng đợi của hệ thống.

Các tùy chọn cũng có thể viết ngắn gọn hơn {–Q|queue} khi đó bạn có thể thay –queue trong câu lệnh artisan thành -Q. Giống như khi chúng ta thực hiện kiểm tra phiên bản của Laravel Artisan:

c:\xampp\htdocs\allaravel.com>php artisan --version Laravel Framework 5.4.23 c:\xampp\htdocs\allaravel.com>php artisan -V Laravel Framework 5.4.23
Code language: CSS (css)

Cả tham số và tùy chọn ở trên đều có thể đưa vào câu lệnh dưới dạng mảng, khi đó chúng ta sử dụng ký tự * để hàm ý các đầu vào là dạng mảng:

/** * The name and signature of the console command. * * @var string */ protected $signature = 'user:delete {user*}';
Code language: PHP (php)

Khi đó thực hiện php artisan user:delete 1 2 3 thì tham số user sẽ là một mảng [1, 2, 3]. Tương tự cho tùy chọn, chúng ta cũng sử dụng *:

/** * The name and signature of the console command. * * @var string */ protected $signature = 'user:delete {--id=*}';
Code language: PHP (php)

Câu lệnh artisan sẽ như sau: php artisan user:delete –id=1 –id=2

Với thuộc tính signature chúng ta cũng có thể đưa vào các text mô tả cho từng tham số hoặc tùy chọn như vậy khi liệt kê danh sách các lệnh artisan chúng ta dễ dàng biết các tham số và tùy chọn này để làm gì.

/** * The name and signature of the console command. * * @var string */ protected $signature = 'user:create {userType : Type of user example admin, user...} {numberOfUser=1 : Number of user will create, default one user created} {--queue}';
Code language: PHP (php)

3.3 Thao tác dữ liệu vào ra của câu lệnh Artisan

Trong phần trên chúng ta đã được giới thiệu cách định nghĩa các tham số và tùy chọn, vậy thao tác với các dữ liệu này như thế nào, lớp Command có các phương thức argument() và option() để lấy các giá trị nhập vào:

/** * Execute the console command. * * @return mixed */ public function handle() { // Lấy tham số dạng người dùng $userType = $this->argument('userType'); // Lấy tất cả các tham số $arguments = $this->arguments(); // Lấy tùy chọn $isQueue = $this->option('queue'); // Lấy tất cả các tùy chọn $options = $this->options(); }
Code language: PHP (php)

3.3.1 Nhập liệu thông tin cho câu lệnh artisan

Nhập thông tin vào câu lệnh có thể thông qua tham số và tùy chọn, tuy nhiên cách này khó với các trường hợp có rẽ nhánh trong quyết định, ví dụ khi thực hiện một công việc nào đó rất quan trọng đòi hỏi phải hỏi kỹ hơn người thực hiện lệnh chúng ta cần in ra các câu hỏi để người dùng lựa chọn. Có rất nhiều các phương thức giúp bạn tạo ra một giải pháp tổng thể cho vấn đề này.

ask(): hiển thị một câu hỏi và yêu cầu người dùng nhập liệu

/** * Execute the console command. * * @return mixed */ public function handle() { $name = $this->ask('What is your name?'); }
Code language: PHP (php)

secret(): dùng cho nhập liệu liên quan đến thông tin cần bảo mật như mật khẩu, mật mã tài khoản… khi đó bạn nhập liệu vào và nó không hiển thị trên màn hình.

$password = $this->secret('What is the password?');
Code language: PHP (php)

confirm(): câu hỏi xác nhận, mặc định phương thức trả về false, tuy nhiên như người dùng nhập vào y hoặc yes thì nó sẽ trả về giá trị true.

if ($this->confirm('Do you wish to continue?')) { // }
Code language: PHP (php)

anticipate(): cho phép auto-completion với các tùy chọn có thể.

$name = $this->anticipate('What is your name?', ['Taylor', 'Dayle']);
Code language: PHP (php)

choice(): Câu hỏi nhiều lựa chọn, các lựa chọn được định nghĩa trước.

$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], $default);
Code language: PHP (php)

3.3.2 Hiển thị thông tin khi thực hiện câu lệnh artisan

Trong quá trình thực thi câu lệnh artisan chúng ta muốn hiển thị các thông báo như câu lệnh đã hoàn thành hoặc câu lệnh gặp lỗi… Rất đơn giản class Command có các phương thức như info(), error(), line():

/** * Execute the console command. * * @return mixed */ public function handle() { try { $this->info('Create user success'); } catch (e Exception) { $this->error('Error in create user'); } }
Code language: PHP (php)

Với các thông tin dạng bảng, phương thức table() sẽ giúp in ra dễ dàng, nếu bạn đã từng sử dụng câu lệnh php artisan route:list bạn sẽ thấy là kết quả hiển thị danh sách các route trong ứng dụng ở dạng bảng, nếu câu lệnh của bạn cũng muốn các dữ liệu dạng bảng được hiển thị, bạn có thể dùng table():

$headers = ['Name', 'Email']; $users = App\User::all(['name', 'email'])->toArray(); $this->table($headers, $users);
Code language: PHP (php)

Hiển thị thanh tiến trình là rất cần thiết với các công việc thực hiện mất nhiều thời gian, một câu lệnh mất đến 5 phút mà không hiển thị gì lên màn hình thì đúng là ác mộng, với thanh tiến trình chúng ta có thể biết được công việc đã thực hiện được bao nhiêu phần trăm.

$users = App\User::all(); $bar = $this->output->createProgressBar(count($users)); foreach ($users as $user) { $this->performTask($user); $bar->advance(); } $bar->finish();
Code language: PHP (php)

3.4 Đăng ký câu lệnh với hệ thống

Với thông tin trong các phần 3.1 đến 3.3 chúng ta đã định nghĩa được các câu lệnh artisan của riêng mình, tiếp theo chúng ta cần đăng ký câu lệnh với hệ thống bằng cách đưa chúng vào thuộc tính commands trong lớp Kernel (app/Console/Kernel.php):

protected $commands = [ Commands\UserCommand::class ];
Code language: PHP (php)

Sau khi đăng ký xong, bạn sẽ thấy thông tin câu lệnh user:create khi thực hiện liệt kê các lệnh artisan (php artisan list) và cũng có thể thực hiện được lệnh php artisan user:create như các lệnh artisan sẵn có.

3.5 Ví dụ tạo câu lệnh artisan cho mục đích riêng

Ok, phần lý thiết như vậy là đã khá chi tiết và đầy đủ, chúng ta cùng bắt tay vào một ví dụ tạo ra một câu lệnh có thể tạo ra các user để test. Chúng ta cùng bắt đầu nhé. Đầu tiên tạo ra lớp UserCommand bằng lệnh php artisan make:command UserCommand. Câu lệnh này sẽ tạo ra UserCommand.php trong thư mục app\Console\Commands:

<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Faker; use Hash; use DB; class UserCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'user:create {userType=admin : Type of user ex: admin, user...} {numberOfUser=1 : Number of user create}'; /** * The console command description. * * @var string */ protected $description = 'Create an user'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $userTypeId = ($this->argument('userType')=='admin')?1:2; $numberOfUser = $this->argument('numberOfUser'); if($userTypeId == 1) { if($this->confirm('Are you sure create administrator?')) { $faker = Faker\Factory::create(); try { for ($i=0; $i < $numberOfUser; $i++) { DB::table('users')->insert([ 'name' => $faker->name, 'email' => $faker->unique()->email, 'user_type_id' => $userTypeId, 'password' => Hash::make('123456') ]); } $this->info($numberOfUser . ($userTypeId==1)?' administrators':' users' . ' create success.'); } catch (Exception $e) { $this->error('Error ' . $e . ' when create users.'); } } } } }
Code language: HTML, XML (xml)

Tiếp theo, để sử dụng được câu lệnh này chúng ta cần đăng ký nó trong app\Console\Kernel.php trong thuộc tính $commands:

<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ Commands\UserCommand::class, ]; ...
Code language: HTML, XML (xml)

Thực hiện liệt kê danh sách các lệnh artisan xem đã có chưa?

c:\xampp\htdocs\allaravel>php artisan list Laravel Framework 5.4.23 ... user user:create Create an user ...
Code language: CSS (css)

Chưa thấy các thông tin về các tham số và tùy chọn của câu lệnh, chúng ta thực hiện với tùy chọn –help hoặc -h:

c:\xampp\htdocs\allaravel.com>php artisan user:create -h Usage: user:create <userType> [<numberOfUser>] Arguments: userType Type of user ex: admin, user... numberOfUser Number of user create [default: "1"] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: Create an user
Code language: PHP (php)

Chúng ta đã thấy các tham số như userType và numberOfUser đã hiển thị, nó thật hữu ích khi muốn tìm hiểu nhanh thông tin một câu lệnh artisan phải không. Tiếp theo chúng ta đi vào phần chính là thực hiện thử lệnh artisan này.

c:\xampp\htdocs\allaravel.com>php artisan user:create admin 10 Are you sure create administrator? (yes/no) [no]: > y 10 administrators create success

Chúng ta cũng thấy khi đưa vào tham số là tạo admin, câu lệnh sẽ hỏi bạn có muốn tạo admin hay không?

4. Thực hiện các câu lệnh Artisan trong mã chương trình

Các câu lệnh artisan có thể thực hiện trong mã chương trình khi thực hiện phương thức call với facade Artisan, trong phần Bật chế độ bảo trì website bằng ứng dụng chúng ta hoàn toàn có thể thực hiện:

Route::get('turn-on-maintanence-mode', function(){ Artisan::call('down'); });
Code language: CSS (css)

Nếu bạn muốn các câu lệnh này thực hiện trong hàng đợi của hệ thống, sử dụng phương thức queue:

Route::get('/foo', function () { Artisan::queue('email:send', [ 'user' => 1, '--queue' => 'default' ]); // });
Code language: PHP (php)

Chúng ta cũng có thể thực hiện việc truyền giá trị cho các tham số và tùy chọn khi thực thi câu lệnh:

$exitCode = Artisan::call('user:create', [ 'userType' => 'admin', 'numberOfUser' => 10 ]);
Code language: PHP (php)

5. Lời kết

Laravel Artisan giúp cho các công việc phần backend dễ chịu hơn rất nhiều, chúng ta hoàn toàn có thể tạo ra những câu lệnh thực hiện các kịch bản giúp việc viết code hoặc quản trị website. Có thể nói Laravel Artisan là một ý tưởng rất hay, một công cụ giúp cho framework Laravel đang tạo ra sự khác biệt so với các framework khác.

Cũng có thể do Taylor Otwell, người phát triển ra framework Laravel là một lập trình viên lâu năm của .NET được sử dụng công cụ Microsoft Visual Studio quá tiện lợi, muốn đưa luồng gió đó vào với ngôn ngữ PHP. Chúng ta hãy cùng chờ xem, liệu Laravel còn những cái gì khác khiến chúng ta phải trầm trồ.

Các bạn có thể tham khảo các bài viết hay về Laravel tại đây.


Hãy tham gia nhóm Học lập trình để thảo luận thêm về các vấn đề cùng quan tâm.

Nguồn tham khảo: allaravel

Leave a Reply

Your email address will not be published.