Site icon บริษัท โค๊ดบี จำกัด

codeigniter hooks ทำความเข้าใจและใช้งาน hooks point

php-codeigniter-framework

php-codeigniter-framework

<h3>codeigniter hooks ทำความเข้าใจและใช้งาน hooks point<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;สำหรับ hooks ใน codeigniter เป็นฟีเจอร์ที่มีไว้สำหรับปรับเปลี่ยนลำดับขั้นตอนการทำงานหรือแทรกความสามารถ<br &sol;>&NewLine;บางอย่างเข้าไปแบบมีลำดับขั้นตอน โดยที่ไม่ต้องเข้าไปยุ่งเกี่ยวกับไฟล์หลัก ตัวอย่างเช่น อาจมีบางกรณีที่เราต้องการ<br &sol;>&NewLine;เพิ่มข้อมูลเข้าไปในส่วนของ controller ใด ๆ ก่อนที่จะเข้าสู่การทำงานของ constructure ฟังก์ชั่น <&excl;--more--><&sol;p>&NewLine;<p>ของ controller นั้น ๆ หรืออาจจะต้องการเพิ่มการทำงานบางอย่างเข้าไปหลังจากที่ constructure ทำงานเสร็จแล้ว<br &sol;>&NewLine;หรือแม้กระทั้ง controller นั้น ๆ ได้ทำงานเสร็จแล้วค่อยเรียกการทำงานบางอย่างเข้าไป ก็สามารถทำได้ด้วย<br &sol;>&NewLine;Hooks class เช่นเดียวกัน<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>เปิดใช้งาน hooks<&sol;h4>&NewLine;<p>สำหรับการเปิดใช้งาน hooks ให้เปิดไฟล์ <strong>application&sol;config&sol;config&period;php<&sol;strong> และเปลี่ยนตัวแปร enable&lowbar;hooks<br &sol;>&NewLine;จากค่าปกติที่เป็น FALSE ให้เป็น TRUE<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้งาน hooks ใน ci" >&dollar;config&lbrack;'enable&lowbar;hooks'&rsqb; &equals; TRUE&semi;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>สร้างไฟล์ hooks class<&sol;h4>&NewLine;<p>การสร้าง hooks คลาสนั้น ให้เราสร้างไว้ในโฟลเด้อ application&sol;hooks&sol; ในที่นี้ให้เราทดลองสร้างไฟล์ WebHook&period;php<br &sol;>&NewLine;โดยที่ภายในคลาสนี้เราจะต้องสร้าง method มาเพื่อระบุการทำงานใน hooks แต่ละขั้นตอนด้วย จะได้โค๊ดประมาณนี้<&sol;p>&NewLine;<p><strong>application&sol;hooks&sol;WebHook&period;php<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"application&sol;hooks&sol;WebHook&period;php" >&lt&semi;&quest;php if &lpar; &excl; defined&lpar;'BASEPATH'&rpar;&rpar; exit&lpar;'No direct script access allowed'&rpar;&semi; &NewLine; &NewLine;class WebHook &lbrace; &NewLine; &NewLine;&Tab;public function &lowbar;&lowbar;construct&lpar;&rpar;&lbrace; &NewLine;&Tab; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function pre&lowbar;system&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "pre&lowbar;system&period;&lt&semi;br&gt&semi;"&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;&quest;&gt&semi;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>เรียกใช้งานแบบเป็นลำดับขั้นตอนของ hooks class<&sol;h4>&NewLine;<p>หลังจากได้คลาสและฟังก์ชั่นที่จะเรียกใช้งานแล้ว ให้เราไปกำหนดลำดับการทำงานที่ไฟล์<br &sol;>&NewLine;สำหรับการสั่งให้คลาส WebHook ทำงานแต่ละเมธอดแบบเป็นลำดับขั้นนั้นเราจะเขียนคำสั่งใน hooks&period;php ได้ดังนี้<&sol;p>&NewLine;<p><strong>application&sol;config&sol;hooks&period;php<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"application&sol;hooks&sol;WebHook&period;php" > &NewLine;<&quest;php &NewLine;defined&lpar;'BASEPATH'&rpar; OR exit&lpar;'No direct script access allowed'&rpar;&semi; &NewLine; &NewLine;&dollar;hook&lbrack;'pre&lowbar;system'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &sol;&sol; ชื่อคลาส &NewLine; 'function' &equals;> 'pre&lowbar;system'&comma; &sol;&sol; ชื่อฟังก์ชั่นที่เรียกใช้งานหรือเมธอด &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &sol;&sol; ชื่อไฟล์ &NewLine; 'filepath' &equals;> 'hooks'&comma; &sol;&sol; โฟลเด้อหรือตำแหน่งของไฟล์ &NewLine; 'params' &equals;> '' &sol;&sol; สำหรับส่งตัวแปรพารามิเตอร์ไปแบบอาเรย์ &NewLine;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>Hook Points<&sol;h4>&NewLine;<p>ในการเรียกใช้งาน hook class นั้น จะต้องมีตัวแปรสำหรับเป็นตัวกำหนดลำดับขั้นตอนการทำงาน<br &sol;>&NewLine;เราเรียกสิ่งนั้นว่า Hook Points ใน codeigniter framework มี Hook Points ให้เราเลือกใช้งานอยู่หลัก ๆ ดังนี้<&sol;p>&NewLine;<ul>&NewLine;<li><strong>pre&lowbar;system<&sol;strong> ถูกเรียกตั้งแต่แรกเริ่มของการทำงานระบบ<&sol;li>&NewLine;<li><strong>pre&lowbar;controller<&sol;strong> ถูกเรียกทันทีก่อนที่ตัวควบคุม &lpar;controller&rpar; ใดๆจะถูกเรียก<&sol;li>&NewLine;<li><strong>post&lowbar;controller&lowbar;constructor<&sol;strong> ถูกเรียกทันทีเมื่อ constructructor ทำงาน<&sol;li>&NewLine;<li><strong>post&lowbar;controller<&sol;strong> ถูกเรียกทันทีเมื่อ controller ทำงานเสร็จสมบรูณ์<&sol;li>&NewLine;<li><strong>post&lowbar;system<&sol;strong> ถูกเรียกหลังสิ้นสุดการทำงานของระบบหลักทั้งหมด<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>hook points อื่น ๆ ท่านสามารถอ่านรายละเอียดเพิ่มเติมได้ที่ <a href&equals;"https&colon;&sol;&sol;www&period;codeigniter&period;com&sol;user&lowbar;guide&sol;general&sol;hooks&period;html" target&equals;"&lowbar;blank">https&colon;&sol;&sol;www&period;codeigniter&period;com&sol;user&lowbar;guide&sol;general&sol;hooks&period;html<&sol;a><br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>สร้าง Controller มาทดสอบการทำงาน<&sol;h4>&NewLine;<p>หลังจากที่เราได้ตั้งค่าเปิดใช้งาน สร้าง hook class และเรียกใช้งานฟังก์ชั่นของ hook class แล้ว<br &sol;>&NewLine;ขั้นตอนต่อไปให้ลองสร้าง controller มาทดสอบการทำงานดูว่าผลลัพธ์จะเป็นอย่างไร<&sol;p>&NewLine;<p><strong>application&sol;controllers&sol;Home&period;php<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"application&sol;controllers&sol;Home&period;php" > &NewLine;<&quest;php &NewLine;defined&lpar;'BASEPATH'&rpar; OR exit&lpar;'No direct script access allowed'&rpar;&semi; &NewLine; &NewLine;class Home extends CI&lowbar;Controller &lbrace; &NewLine; &NewLine;&Tab;public function &lowbar;&lowbar;construct&lpar;&rpar; &NewLine;&Tab;&lbrace; &NewLine;&Tab;&Tab;parent&colon;&colon;&lowbar;&lowbar;construct&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function index&lpar;&rpar; &NewLine;&Tab;&lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>ผลลัพธ์<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >pre&lowbar;system<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>ทดสอบลำดับขั้นตอนการทำงานของ Hook Points<&sol;h4>&NewLine;<p>เขียนฟังก์ชั่นเพิ่มเติมสำหรับเรียกใช้งานแต่ละ point ใน application&sol;hooks&sol;WebHook&period;php ดังนี้<br &sol;>&NewLine;<strong>application&sol;hooks&sol;WebHook&period;php<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"application&sol;hooks&sol;WebHook&period;php" > &NewLine;<&quest;php if &lpar; &excl; defined&lpar;'BASEPATH'&rpar;&rpar; exit&lpar;'No direct script access allowed'&rpar;&semi; &NewLine; &NewLine;class WebHook &lbrace; &NewLine; &NewLine;&Tab;public function &lowbar;&lowbar;construct&lpar;&rpar;&lbrace; &NewLine;&Tab; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function pre&lowbar;system&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "pre&lowbar;system&period;<br>"&semi; &NewLine;&Tab;&rcub;&Tab; &NewLine;&Tab;public function pre&lowbar;controller&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "pre&lowbar;controller&period;<br>"&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function post&lowbar;controller&lowbar;constructor&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "post&lowbar;controller&lowbar;constructor&period;<br>"&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function post&lowbar;controller&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "post&lowbar;controller&period;<br>"&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab;public function post&lowbar;system&lpar;&rpar;&lbrace; &NewLine;&Tab; &Tab;echo "post&lowbar;system&period;<br>"&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine;&quest;> &NewLine;<&sol;pre>&NewLine;<p>เรียกใช้งานฟังก์ชั่นแบบลำดับขั้นตอนโดยกำหนด hook points ที่ไฟล์ application&sol;config&sol;hooks&period;php ดังนี้<br &sol;>&NewLine;<strong>application&sol;config&sol;hooks&period;php<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"application&sol;config&sol;hooks&period;php" > &NewLine;<&quest;php &NewLine;defined&lpar;'BASEPATH'&rpar; OR exit&lpar;'No direct script access allowed'&rpar;&semi; &NewLine; &NewLine;&dollar;hook&lbrack;'pre&lowbar;system'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &NewLine; 'function' &equals;> 'pre&lowbar;system'&comma; &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &NewLine; 'filepath' &equals;> 'hooks'&comma; &NewLine; 'params' &equals;> '' &NewLine;&rpar;&semi; &NewLine;&dollar;hook&lbrack;'pre&lowbar;controller'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &NewLine; 'function' &equals;> 'pre&lowbar;controller'&comma; &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &NewLine; 'filepath' &equals;> 'hooks'&comma; &NewLine; 'params' &equals;> '' &NewLine;&rpar;&semi; &NewLine;&dollar;hook&lbrack;'post&lowbar;controller&lowbar;constructor'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &NewLine; 'function' &equals;> 'post&lowbar;controller&lowbar;constructor'&comma; &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &NewLine; 'filepath' &equals;> 'hooks'&comma; &NewLine; 'params' &equals;> '' &NewLine;&rpar;&semi; &NewLine;&dollar;hook&lbrack;'post&lowbar;controller'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &NewLine; 'function' &equals;> 'post&lowbar;controller'&comma; &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &NewLine; 'filepath' &equals;> 'hooks'&comma; &NewLine; 'params' &equals;> '' &NewLine;&rpar;&semi; &NewLine;&dollar;hook&lbrack;'post&lowbar;system'&rsqb; &equals; array&lpar; &NewLine; 'class' &equals;> 'WebHook'&comma; &NewLine; 'function' &equals;> 'post&lowbar;system'&comma; &NewLine; 'filename' &equals;> 'WebHook&period;php'&comma; &NewLine; 'filepath' &equals;> 'hooks'&comma; &NewLine; 'params' &equals;> '' &NewLine;&rpar;&semi; &NewLine; &NewLine;<&sol;pre>&NewLine;<p>ผลลัพธ์<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " > &NewLine;pre&lowbar;system&period; &NewLine;pre&lowbar;controller&period; &NewLine;post&lowbar;controller&lowbar;constructor&period; &NewLine;post&lowbar;controller&period; &NewLine;post&lowbar;system&period; &NewLine;<&sol;pre>&NewLine;<p><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;07&sol;วิธีใช้งาน-hooks-ใน-codeigniter&period;png" alt&equals;"วิธีใช้งาน-hooks-ใน-codeigniter" &sol;><br &sol;>&NewLine;หลังจากที่เราทดสอบการทำงานแบบนี้แล้ว จะทำให้เราทราบว่า hook point ไหน ทำงานก่อน - หลัง และทำงานตอนไหน<br &sol;>&NewLine;เมื่อทราบอย่างนี้แล้วก็จะสามารถทำความสามารถของ hooks ใน codeigniter framework ไปประยุกต์ใช้ในการทำงาน<br &sol;>&NewLine;พัฒนาเว็บไซต์ในรูปแบบต่าง ๆ ได้ดียิ่งขึ้น และยังเป็นการใช้ความสามารถของ codeigniter ได้อย่างเต็มที่อีกด้วย<&sol;p>&NewLine;

Exit mobile version