สมมติว่า มี 2 commands คือ command A และ B แล้วเราสร้าง command C มาอีกตัว เพื่อเรียกใช้ 2 commands นี้
ถ้าหากเป็นการเรียกรัน command จาก controller ปกติ ก็จะใช้คำสั่งดังนี้
1 2 | Artisan::call( 'A' ); Artisan::call( 'ฺB' ); |
แต่สำหรับการเรียกรัน command จากอีก command นึง ห้ามใช้ Artisan::call
โดยเด็ดขาด เนื่องจากถ้าหากเกิด exception ขึ้น มันจะไม่ throw exception ออกมายัง command C ทำให้ command C สามารถรันต่อจนเสร็จได้ ถึงแม้จะเกิด exception ใน command A หรือ B ซึ่งเราจะไม่สามารถรู้ได้เลยว่า มันทำงานเสร็จแล้วจริงๆหรือมันเกิด exception ขึ้น ถ้าเราไม่ดูจาก error logsในความเป็นจริงแล้ว เราควรจะรู้ว่า command ใดเกิด exception ขึ้น และไม่มีความจำเป็นที่ต้องรัน command อื่นๆต่อไป ในกรณีที่ exception ได้เกิดขึ้นแล้ว ดังนั้นให้ใช้
$this->call
แทนดังนี้1 2 | $this ->call( 'A' ); $this ->call( 'ฺB' ); |
อ่านเพิ่มเติม: https://laravel.com/docs/5.7/artisan#calling-commands-from-other-commands
No comments:
Post a Comment