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

สอน Yii 2 : ใช้งาน Model ร่วมกับ View และ Controller

yii2-framework

yii2-framework

<h3>สอน Yii 2 &colon; ใช้งาน Model ร่วมกับ View และ Controller<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;หน้าที่หลักของ models คือการจัดการกับชุดข้อมูลต่าง ๆ ภายในเว็บไซต์ จากนั้นก็รอรับคำสั่งให้จากส่วน controller<br &sol;>&NewLine;เพื่อเริ่มทำงาน ทั้งการส่งข้อมูลจากการร้องขอและการบันทึกข้อมูล คำว่าข้อมูลในทีนี้หมายถึงชุดข้อมูลในรูปแบบต่าง ๆ<br &sol;>&NewLine;ไม่ว่าจะเป็นไฟล์ข้อมูล เช่น xml ข้อมูลที่ถูกดึงขึ้นมาจากฐานข้อมูล และรวมไปถึงข้อมูลที่ถูกสร้างขึ้นที่โมเดลเอง<br &sol;>&NewLine; <&excl;--more--><br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;ในบทความที่แล้ว <a href&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;สอน-yii-2-เริ่มต้นสร้าง-view-และ-controller&sol;" target&equals;"&lowbar;blank">เริ่มต้นสร้าง View และ Controller<&sol;a> เราได้ทำความเข้าใจและใช้งานส่วนของ view และ controller<br &sol;>&NewLine;ในบทความนี้ จะใช้ไฟล์โปรเจคเดิมที่เราได้สร้างไว้ คือ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default highlight&colon;0 decode&colon;true " >controllers&sol;UserController&period;php &NewLine;views&sol;user&sol;index&period;php<&sol;pre>&NewLine;<p>แต่จะเพิ่มให้มีการทำงานร่วมกับ model ที่เราจะต้องสร้างไว้ในโฟลเด้อ <strong>models&sol;User&period;php<&sol;strong><br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>สร้าง User Models<&sol;h4>&NewLine;<p>ให้เราสร้างไฟล์ User&period;php วางไว้ใน <strong>models&sol;User&period;php<&sol;strong> เพื่อใช้งานร่วมกับ UserController โดยเขียนโค๊ดได้ดังนี้<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"models&sol;User&period;php">namespace app&bsol;models&semi; &NewLine; &NewLine;use Yii&semi; &NewLine;use yii&bsol;base&bsol;Model&semi; &sol;&sol; เปิดการเข้าถึง Model class &NewLine; &NewLine;class User extends Model &sol;&sol; สืบทอดคุณสมบัติของ Model Class &NewLine;&lbrace; &NewLine; public &dollar;name &equals; "Witoon Pomipon"&semi; &NewLine; public &dollar;email &equals; "witoon&commat;codebee&period;co&period;th"&semi; &NewLine; &NewLine; public function getName&lpar;&rpar; &NewLine; &lbrace; &NewLine; return &dollar;this-&gt&semi;name&semi; &NewLine; &rcub; &NewLine;&Tab;public function getEmail&lpar;&rpar; &NewLine; &lbrace; &NewLine; return &dollar;this-&gt&semi;email&semi; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<p>จากโค๊ดด้านบนเราได้สร้าง User คลาสและสืบทอดคุณสมบัติ Model คลาสซึ่งเป็นคลาสต้นแบบของ Yii2 จากนั้นได้สร้าง<br &sol;>&NewLine;Method มา 2 ตัวคือ getName และ getEmail พร้อมกับ return ค่าออกไปเมื่อมีการเรียกใช้งาน<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>ประกาศ User Model ใน UserContrller<&sol;h4>&NewLine;<p>หลังจากที่เราได้สร้างไฟล์ User&period;php ซึ่งเป็น model ข้อมูลเกี่ยวกับสมาชิกเรียบร้อยแล้ว ต่อมาจะเป็นการเรียกใช้งาน<br &sol;>&NewLine;ประกาศตัวแปร User Model ใน UserController จะสามารถเขียนโค๊ดดังตัวอย่างด้านล่างครับ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >namespace app&bsol;controllers&semi; &NewLine; &NewLine;use Yii&semi; &NewLine;use yii&bsol;web&bsol;Controller&semi; &NewLine;use app&bsol;models&bsol;User&semi; &sol;&sol; เปิดการเข้าถึง User Model &NewLine; &NewLine;class UserController extends Controller &NewLine;&lbrace; &NewLine; public function actionIndex&lpar;&rpar; &NewLine; &lbrace; &NewLine;&Tab;&Tab;&dollar;User &equals; new User&lpar;&rpar;&semi; &sol;&sol; ประกาศตัวแปร &dollar;User มีค่าเท่ากับ User Model &NewLine; return &dollar;this-&gt&semi;render&lpar;'index'&comma; &lbrack; &sol;&sol; สั่ง render หน้า index view &NewLine; 'User' &equals;&gt&semi; &dollar;User&comma; &sol;&sol; พร้อมกับส่ง User Model ออกไปด้วย &NewLine; &rsqb;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<p>จากโค๊ดด้านบนเราได้เปิดการเข้าถึงไฟล์ User Model จากนั้นก็ประกาศตัวแปร &dollar;User และสั่ง render หน้า index<br &sol;>&NewLine;พร้อมกับส่งตัวแปร &dollar;User ออกไปที่ index view<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>แสดงข้อมูล User Model ที่ View<&sol;h4>&NewLine;<p>จะสังเกตุเห็นว่าการส่งข้อมูลออกไปที่ view ของ Yii framework มีหลักการเดียวกับ Laravel และ CI คือส่งออกไป<br &sol;>&NewLine;ในรูปแบบ Array และตอนนี้ใน views&sol;user&sol;index&period;php เราก็จะสามารถใช้งาน Method ต่าง ๆ ของ User Model<br &sol;>&NewLine;ได้ผ่านตัวแปร &dollar;User ที่ถูกส่งออกมาจาก UserController ดังตัวอย่างโค๊ดด้านล่าง<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;div class&equals;"site-about"&gt&semi; &NewLine; &lt&semi;p&gt&semi; &NewLine; Hi&comma; I am beginner in Yii นาจาา &lt&semi;&quest;php echo &dollar;User-&gt&semi;getName&lpar;&rpar;&period;" &sol; "&period;&dollar;User-&gt&semi;getEmail&lpar;&rpar;&semi; &quest;&gt&semi; &NewLine; &lt&semi;&sol;p&gt&semi; &NewLine;&lt&semi;&sol;div&gt&semi;<&sol;pre>&NewLine;<p>ผลลัพธ์ที่แสดงบนเว็บเบราว์เซอร์ คือ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default highlight&colon;0 decode&colon;true " >Hi&comma; I am beginner in Yii นาจาา Witoon Pomipon &sol; witoon&commat;codebee&period;co&period;th<&sol;pre>&NewLine;<p>ตัวอย่างการทำงาน model view controller ด้านบนเป็นการยกตัวอย่างแบบง่าย ๆ ไม่ซับซ้อน เพื่อให้ทุกท่านสามารถ<br &sol;>&NewLine;เข้าใจโครงสร้างการทำงานของ Yii ได้ในเบื้องต้นก่อน บทความต่อไปเราจะลองมาทดสอบการทำงานร่วมกับฐานข้อมูลครับ<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;

Exit mobile version