- รันคำสั่ง
sudo certbot certonly -d {domain_name} - เลือก option ที่ 2 เพื่อใช้วิธีการระบุ path ของ public folder ของเว็บแทนการสร้าง standalone web server ขึ้นมา
- ใส่ path ของ public folder แล้วกด enter
Menu
Tuesday, February 5, 2019
[Server - Ubuntu] การสร้างหรือต่ออายุ Let's Encrypt Certificate สำหรับ Website
[Server - Ubuntu] How to Create or Renew Let's Encrypt Certificate for Website
สมมติว่า ได้ลง certbot ไว้ในเครื่อง server เรียบร้อยแล้ว
[Laravel - Command] วิธีการเรียกใช้ command ที่ถูกต้อง จากอีก command นึงใน Laravel
[Laravel - Command] How to Call Laravel Command from Other Commands
บางครั้งเราต้องการสร้าง command เพื่อเรียกใช้ชุด command ที่ได้สร้างไว้ก่อนหน้าแล้ว
สมมติว่า มี 2 commands คือ command A และ B แล้วเราสร้าง command C มาอีกตัว เพื่อเรียกใช้ 2 commands นี้
ถ้าหากเป็นการเรียกรัน command จาก controller ปกติ ก็จะใช้คำสั่งดังนี้
ในความเป็นจริงแล้ว เราควรจะรู้ว่า command ใดเกิด exception ขึ้น และไม่มีความจำเป็นที่ต้องรัน command อื่นๆต่อไป ในกรณีที่ exception ได้เกิดขึ้นแล้ว ดังนั้นให้ใช้
อ่านเพิ่มเติม: https://laravel.com/docs/5.7/artisan#calling-commands-from-other-commands
สมมติว่า มี 2 commands คือ command A และ B แล้วเราสร้าง command C มาอีกตัว เพื่อเรียกใช้ 2 commands นี้
ถ้าหากเป็นการเรียกรัน command จาก controller ปกติ ก็จะใช้คำสั่งดังนี้
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 แทนดังนี้$this->call('A');
$this->call('ฺB');
อ่านเพิ่มเติม: https://laravel.com/docs/5.7/artisan#calling-commands-from-other-commands
Tuesday, January 15, 2019
[Laravel - Model] การแปลง Query Builder ไปเป็น SQL Query String
[Laravel - Model] How to Convert Query Builder to SQL Query String
สาเหตุที่ต้องแปลง Query Builder ไปเป็น SQL Query String ก็มีหลายสาเหตุด้วยกัน เช่น การ debug query ที่เกิด error ขึ้น เป็นต้น ซึ่งสามารถใช้โค้ดได้ตามด้านล่างนี้
$sql= str_replace(array('?'), array('\'%s\''), $query->toSql());
$sql = vsprintf($sql, $query->getBindings());
เมื่อ $query คือ Query Builder และ $sql คือ Query String ที่ได้จากการแปลง
Saturday, May 26, 2018
[Ubuntu] การแตกไฟล์ zip ด้วย encoding ที่กำหนด
[Ubuntu] How to Extract ZIP File with Specific Encoding
หลังจากที่บีบอัดไฟล์ที่มีชื่อภาษาไทยเป็นไฟล์ zip บน Windows แล้วนำมาแตกไฟล์บน Ubuntu เพื่อใช้งาน จะพบว่า ชื่อไฟล์ที่เป็นอักขระภาษาไทยถูกแทนที่ด้วยอักระพิเศษที่อ่านไม่รู้เรื่อง
สาเหตุที่เป็นเช่นนั้น เนื่องจากการแตกไฟล์ปกติจะใช้ default encoding ซึ่งไม่ใช่ encoding สำหรับภาษาไทย ดังนั้นเราจึงต้องกำหนด encoding ที่จะใช้ในการแตกไฟล์ดังนี้
Ref: https://superuser.com/questions/872596/decompress-zip-with-given-encoding
สาเหตุที่เป็นเช่นนั้น เนื่องจากการแตกไฟล์ปกติจะใช้ default encoding ซึ่งไม่ใช่ encoding สำหรับภาษาไทย ดังนั้นเราจึงต้องกำหนด encoding ที่จะใช้ในการแตกไฟล์ดังนี้
unzip -O <encoding> <filename> -d <target_dir>ตัวอย่างเช่น
unzip -O windows-874 lp-real-font.zip -d ocr_inสำหรับชื่อ encoding ที่ใช้ได้ สามารถดูได้จาก https://www.iana.org/assignments/character-sets/character-sets.xhtml
Ref: https://superuser.com/questions/872596/decompress-zip-with-given-encoding
[Laravel - Config] การบังคับให้ Laravel สร้าง URL แบบ HTTPS
[Laravel - Config] How to Force Laravel to Create URL with HTTPS
เมื่อ Laravel ถูกดีพลอยบน server ที่มีการเรียกหน้าเว็บแบบ HTTP เบื้องหลัง NGINX ที่ตั้งค่าให้เรียกหน้าเว็บแบบ HTTPS เราจึงจำเป็นต้องบังคับให้ link ที่สร้างขึ้นมาโดย Laravel เป็น HTTPS ทั้งหมด
Method ที่เกี่ยวข้องกับการสร้าง link ของ Laravel มี 2 methods ดังนี้
Method ที่เกี่ยวข้องกับการสร้าง link ของ Laravel มี 2 methods ดังนี้
- url()
- เป็น method สำหรับสร้าง url จาก relative path ที่ใส่เข้าไป เช่น
url('/images/logo_sm.png')เป็นต้น - url ที่ถูกสร้างขึ้นจะเป็น HTTP หรือ HTTPS ตาม server ที่มันอยู่
- วิธีแก้ไข คือ เพิ่ม
\URL::forceScheme('https');ใน boot ของ AppServiceProvider - link()
- เป็น method สำหรับสร้าง render template เพื่อแสดงรายการของหน้า
- Method นี้เป็นของตัวแปรที่ได้มาจากการเรียกใช้ paginate() ตอน query ข้อมูลจากฐานข้อมูล
- วิธีแก้ไข คือ ให้ทำการสั่ง setPath() ให้กับตัวแปรดังกล่าวก่อนที่จะเรียกใช้ link() เช่น
$users->setPath('/users/manage');แล้วจึงเรียกใช้{{$users->link()}}ใน blade template เมื่อ $users เป็นตัวแปรที่ได้มาจากการเรียกใช้ paginate()
Subscribe to:
Posts (Atom)