เฟรมเวิร์ก PHP ที่เบาและใช้งานง่าย
DeJframework เป็นกรอบ PHP MVC ที่เรียบง่ายและน้อยที่สุดที่มุ่งเน้นไปที่ความกระชับความเรียบง่ายและความเข้าใจ ไวยากรณ์ส่วนใหญ่ได้รับแรงบันดาลใจจาก Laravel DeJframework ได้รับการพัฒนาโดย ATA Marzban สำหรับโครงการสุดท้ายของปริญญาตรี
#การติดตั้ง
composer install ในไดเรกทอรีรูทเพื่อให้เฟรมเวิร์กทราบว่าจะทำอย่างไรเมื่อมีการร้องขอคุณควรตั้งเส้นทางสำหรับแอปพลิเคชันของคุณใน /App/Routes.php ทำได้ง่าย
เส้นทางประกอบด้วย วิธี HTTP, URL และปลายทางที่ควรดำเนินการหากมีการร้องขอด้วยวิธีการที่ระบุไปยัง URL ที่ระบุ ไม่มีอะไรถูกดำเนินการโดยไม่ต้องตั้งค่าเส้นทาง คุณสามารถผ่านการปิดเป็นปลายทาง:
[/app/routes.php]
Route:: set ( " GET " , " / " , function (){
return " Hello World! " ;
});รหัสนี้จะส่งออก "Hello World" หากคุณเยี่ยมชมเว็บไซต์ของคุณบน Yoursite.com
[/app/routes.php]
Route:: set ( " GET " , " /some/url " , function (){
return " This is some url! " ;
});รหัสนี้จะส่งออก "นี่คือ URL!" หากคุณไปเยี่ยมชมเว็บไซต์ของคุณบนของคุณ/some/url
[/app/routes.php]
Route:: set ( " POST " , " /do/something " , function (){
return " Here you should write your own code to do the things you want. " ;
});รหัสนี้จะถูกเรียกใช้งานหากคุณเยี่ยมชมของคุณ/dodo/Something ด้วยวิธีการโพสต์ (จากแบบฟอร์มหรือการโทร API)
คุณสามารถส่งคืนสตริงวัตถุอาร์เรย์หรือมุมมอง (กล่าวถึงในภายหลัง) ในการปิดหรือตัวควบคุมของคุณ สตริงจะถูกส่งออกโดยตรงในขณะที่วัตถุหรือมุมมองจะถูกแปลงเป็น JSON และเอาต์พุตโดยอัตโนมัติและมุมมองจะถูกแสดงเป็นเอาต์พุต
ในส่วนถัดไปคุณจะได้เรียนรู้วิธีส่งเส้นทางไปยังคอนโทรลเลอร์แทนการปิด
ถือว่าเป็นแนวปฏิบัติที่ดีที่สุดในรูปแบบ MVC ที่ควรใส่ตรรกะแอปพลิเคชันลงในตัวควบคุม
[/app/controllers/YourController.php]
<?php
namespace app controllers;
class YourController extends dej mvc Controller
{
public static function yourAction ()
{
return " This is the right way to do it! " ;
}
}สำคัญ: อย่างที่คุณเห็นในตัวอย่างด้านบนเพื่อให้ AutoLoader PSR-0 ทำงานได้อย่างถูกต้องคุณควรทำตามอนุสัญญาต่อไปนี้เมื่อเพิ่มคลาสใด ๆ ลงในแอปพลิเคชันของคุณ:
ตอนนี้เรามาเรียนรู้วิธีการทำงานของตัวควบคุมต่อไป:
[/app/routes.php]
Route:: set ( " GET " , " / " , " YourController@YourAction " );การดำเนินการที่ระบุในคอนโทรลเลอร์ที่ระบุจะถูกดำเนินการเมื่อเส้นทางทริกเกอร์
การยกระดับชั้นเรียนอย่างต่อเนื่องและผ่านการพึ่งพาพวกเขาสามารถกลายเป็นงานซ้ำ ๆ ในการพัฒนา PHP ผู้ให้บริการ dej App มีจุดมุ่งหมายเพื่อให้กระบวนการนี้แห้งที่สุดเท่าที่จะทำได้ การใช้ผู้ให้บริการนี้คุณไม่จำเป็นต้องเพิ่มคำสั่ง การใช้ ในแต่ละไฟล์และการพึ่งพาผ่าน ทำตัวอย่างนี้:
/**
* Without a Service Provider
* when you want to build a query
*/
use dej db Connection ;
$ connection = Connection:: getInstance ();
use dej db Query ;
$ query = new Query ( $ connection );
$ result = $ query -> select ()-> from ( ' someTable ' )-> getAll ();
return $ result ;และสิ่งนี้ควรทำซ้ำทุกครั้งที่คุณต้องการใช้ตัวสร้างแบบสอบถาม ตอนนี้ใช้ผู้ให้บริการ:
use dej App ;
return App:: Query ()-> select ()-> from ( ' some_table ' );แค่ไหน! ดูที่ dej/app.php เพื่อดูว่ามันทำงานอย่างไร วิธีการคงที่ชื่อ 'Query' ถูกเรียกในคลาสแอพ มันอินสแตนซ์คลาสการสืบค้นและส่งผ่านอินสแตนซ์การเชื่อมต่อเป็นพารามิเตอร์ตัวสร้างกับมัน ชิ้นเค้ก!
#request dejhttpRequest class ทำให้การโต้ตอบกับคำของ่าย ๆ ลองดูตัวอย่างต่อไปนี้:
//check if request is ajax or not
$ result = App:: Request ()-> isAjax ();
//returns $_GET['name'];
$ result = App:: Request ()-> get ( ' name ' );
//returns $_POST['name'];
$ result = App:: Request ()-> post ( ' name ' );
//returns $_REQUEST;
$ result = App:: Request ()-> all (); #Response คลาส dejhttpResponse ทำให้พารามิเตอร์การตั้งค่าการตอบสนองง่ายคุณควรส่งคืนการตอบกลับในคอนโทรลเลอร์ของคุณหากคุณต้องการตั้งค่ารหัสการตอบกลับ HTTP หรือส่วนหัวให้ดูตัวอย่างต่อไปนี้:
class IndexController extends dej mvc Controller
{
public static function index ()
{
return App:: Response ()-> code ( 404 )-> header ( ' HTTP/1.1 404 Not Found ' );
}
}คุณสามารถเปลี่ยนเส้นทางได้อย่างง่ายดาย:
//in the controller
return App:: Response ()-> redirect ( ' /login ' );
//redirect with errors
App:: Response ()-> redirect ( ' / ' )-> withErrors ([ ' login ' => ' login unsuccessful! maybe password is wrong. ' ]);การเปลี่ยนเส้นทางด้วยข้อผิดพลาดกะพริบข้อผิดพลาดไปยังเซสชัน (กล่าวถึงในภายหลัง)
การกำหนดค่า: ก่อนอื่นป้อนการกำหนดค่าฐานข้อมูลใน /config.json DeJframework เกี่ยวข้องกับฐานข้อมูลในสถาปัตยกรรม 3 ชั้น:
เลเยอร์ 1 - วัตถุการเชื่อมต่อฐานข้อมูล: สิ่งนี้ขยายคลาสนามธรรม Singleton นั่นหมายความว่ามันเป็นอินสแตนซ์เพียงครั้งเดียวครั้งแรกที่เรียกว่า บริการอื่น ๆ เกี่ยวกับ dejframework ก็เป็นเช่นนี้เช่นกัน เพื่อป้องกันค่าใช้จ่ายในการเชื่อมต่อกับฐานข้อมูลทุกครั้งที่คุณต้องการเรียกใช้แบบสอบถาม นี่คือวิธีที่คุณสามารถใช้งานได้:
//simple query
$ result = App:: Connection ()-> executeQuery ( " SELECT * FROM some_table " );
//NonQuery: a query that doesn't return rows, only the number of affected rows.
$ result = App:: Connection ()-> executeNonQuery ( " DELETE FROM some_table WHERE some_field = 'some_value' " );
//A Query using prepared statements, To protect against SQL Injection.
$ result = App:: Connection ()-> executeQuery ( " SELECT * FROM some_table WHERE some_field = ? " , [ " some_value " ]);
//A Query using prepared statements, To protect against SQL Injection. With Multiple Parameters.
$ result = App:: Connection ()-> executeQuery ( " SELECT * FROM some_table WHERE some_field = ? AND another_field = ? " , [ $ some_value , " another_value " ]);
//A Query using prepared statements, To protect against SQL Injection. With Named Parameters.
$ result = App:: Connection ()-> executeQuery ( " SELECT * FROM some_table WHERE some_field = :some_value_name
AND another_field = :another_value_name " ,
[ " :some_value_name " => $ some_value ,
" another_value_name " => " another_value " ]); คุณสามารถทำได้ทุกที่หากคุณได้เพิ่ม use dejApp; -
Layer 2 - Query Builder: คลาสนี้สร้างแบบสอบถามและใช้คลาสการเชื่อมต่อเพื่อเรียกใช้โดยใช้คำสั่งที่เตรียมไว้อย่างปลอดภัย มันควรจะเป็นอินสแตนซ์สำหรับการสืบค้นใหม่แต่ละครั้งสิ่งนี้ทำเพื่อคุณโดย /dej /แอปโดยอัตโนมัติทุกครั้งที่คุณพิมพ์ App::Query() เช่นเดียวกับที่เราเห็นในตัวอย่างส่วนบริการ Povider คุณสามารถสร้างการสืบค้นได้โดยใช้วิธีการผูกมัด ดูตัวอย่างด้านล่าง:
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> getAll ();
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> getOne ();
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> getJson ();
$ query = App:: Query ()-> select ()-> from ( ' users ' )-> getQuery (); อย่างที่คุณเห็นการใช้ตัวสร้างการสืบค้น DEJ นั้นง่าย ๆ ใช้แอพโทร :: query () และมันจะส่งผ่านคลาสแบบสอบถามที่ฉีดขึ้นใหม่โดยอัตโนมัติไปยังคุณจากนั้นคุณก็ใช้วิธีการที่จะเพิ่มเงื่อนไขที่คุณต้องการ ( getOne() getAll() () getJson() ) หรือแบบสอบถามที่สร้างขึ้น ( getQuery() ) ผลลัพธ์จะถูกดึงในรูปแบบ stdclass ที่คุณสามารถใช้งานได้ง่าย เป็นที่น่าสังเกตว่าหากไม่ได้ใช้วิธีการหนึ่งในการสอบถามในตอนท้ายของการสืบค้นคุณผลลัพธ์จะไม่ถูกดึงออกมา นอกจากนี้คุณสามารถเชนวิธีการหลายบรรทัดและในหลายขั้นตอนตัวอย่างเช่นการเปลี่ยนแปลงโดยเงื่อนไขบางอย่าง:
$ query = App:: Query ()-> select ();
if ( $ somecondition == true ) $ query -> from ( ' users ' );
else $ query -> from ( ' another_table ' );
$ result = $ query -> getAll ();มาดูวิธีอื่น ๆ ที่มีอยู่ในตัวสร้างแบบสอบถามในตัวอย่างต่อไปนี้:
//All queries will be executed using prepared statements and parameters will be handled automatically.
//SELECT Queries:
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> where ( ' id ' , ' = ' , ' 22 ' )-> getAll ();
//you can omit the operator and it uses '=' by default
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> where ( ' id ' , ' 22 ' )-> getAll ();
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> where ( ' city ' , ' = ' , ' Berlin ' )
-> andWhere ( ' age ' , ' > ' , ' 20 ' )-> getAll ();
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> where ( ' city ' , ' = ' , ' Berlin ' )
-> orWhere ( ' city ' , ' = ' , ' Paris ' )-> getAll ();
$ result = App:: Query ()-> select ()-> from ( ' users ' )-> orderBy ( ' age ' , ' DESC ' )
-> limit ( 25 )
-> offset ( 50 )-> getAll ();
//INSERT Query:
$ affectedRows = App:: Query ()-> insertInto ( ' users ' )-> values ([ " username " => " jameshetfield " ,
" password " => " 19831983 " ,
" city " => " Downey " ])-> execute (); โปรดทราบว่า แบบสอบถามที่ไม่ส่งคืนผลลัพธ์จะต้องดำเนินการด้วย php execute() และจะส่งคืนจำนวนแถวที่ได้รับผลกระทบโดยอัตโนมัติ
//UPDATE Query:
$ affectedRows = App:: Query ()-> update ( ' users ' )-> set ([ " age " => 53 ,
" band " => " Metallica " ])
-> where ( ' username ' , ' = ' , ' jameshetfield ' )-> execute ();
//DELETE Query:
$ affectedRows = App:: Query ()-> deleteFrom ( ' users ' )-> where ( ' username ' , ' = ' , ' someone ' )-> execute ();โปรดทราบว่า การลบหรืออัปเดตการสืบค้นอาจส่งผลให้สูญเสียข้อมูลหากไม่มีข้อระบุไว้ในที่ใดเป็นมาตรการรักษาความปลอดภัย Dejframework จะทำการยกเว้นหากพบสถานการณ์ดังกล่าว โปรดเรียกใช้การสืบค้นดังกล่าวโดยใช้คลาสการเชื่อมต่อด้วยตนเอง
เลเยอร์ 3 - การทำแผนที่ความสัมพันธ์เชิงวัตถุ: ORM จะถูกกล่าวถึงในส่วนถัดไป
ตามวิกิพีเดีย:
การทำแผนที่ Object-Relational (ORM) ... เป็นเทคนิคการเขียนโปรแกรมสำหรับการแปลงข้อมูลระหว่างระบบประเภทที่เข้ากันไม่ได้ในภาษาการเขียนโปรแกรมเชิงวัตถุ สิ่งนี้จะสร้างผลเป็น "ฐานข้อมูลวัตถุเสมือนจริง" ที่สามารถใช้ได้จากภายในภาษาการเขียนโปรแกรม
เนื่องจาก dejframework ทำงานในสถาปัตยกรรม MVC การคงอยู่ของข้อมูลจึงมีอยู่ในองค์ประกอบโมเดลของ MVC โมเดลเกี่ยวข้องกับเอนทิตีในแอปพลิเคชันของคุณเช่นผู้ใช้การซื้อผลิตภัณฑ์ ฯลฯ ในระบบการช็อปปิ้ง พวกเขาจำเป็นต้องได้รับการบันทึกและเรียกคืนไปและกลับจากฐานข้อมูล ฐานข้อมูลเชิงสัมพันธ์ทำงานในภาษา SQL และ dejframework เช่นเฟรมเวิร์กส่วนใหญ่ทำงานในสภาพแวดล้อมเชิงวัตถุ และรหัส SQL ที่อยู่ตรงกลางของรหัส PHP ถือว่าไม่เป็นวิธีปฏิบัติที่ดีที่สุด ดังนั้น dejframework พยายามแยกคุณออกจากรหัส SQL ใน 3 ระดับ 2 ของพวกเขาได้รับการอธิบายในส่วนก่อนหน้านี้ตอนนี้เราจะเห็นวิธีการทำงานกับโมเดล:
class User extends dej mvc Model
{
protected static $ dbTable = " users " ;
//Format: ["db_field_name" => "modelPropertyName"]
protected static $ primaryKey = [ " id " => " id " ];
//Model properties
public $ username ;
public $ password ;
public $ city ;
.
.
.
} class User extends dej mvc Model
{
protected static $ dbTable = " users " ;
//Format: ["db_field_name" => "modelPropertyName"]
protected static $ dbFields = [ " username " => " username " ,
" password " => " password " ,
" city " => " city " ,
" id " => " id " ];
public $ username ;
public $ password ;
public $ city ;
.
.
.
} โปรดทราบว่า คุณสมบัติ id ถูกกำหนดไว้ในคลาส /dej/mvc/Model ที่โมเดลของคุณขยาย ดังนั้นหากคุณต้องการให้แบบจำลองของคุณมี ID ไม่จำเป็นต้องกำหนดอีกครั้ง
class User extends dej mvc Model
{
protected static $ dbTable = " users " ;
protected static $ dbFields = [ " username " => " username " ,
" password " => " password " ,
" city " => " city " ,
" id " => " id " ];
//Exactly the same as the class name
protected static $ modelName = " User " ;
public $ username ;
public $ password ;
public $ city ;
.
.
.
}ตอนนี้ ถ้าคุณตั้งค่าการกำหนดค่าเหล่านี้อย่างถูกต้องคุณจะสามารถใช้วิธี ORM ในโมเดลของคุณได้อย่างถูกต้อง ดูตัวอย่างด้านล่าง:
//Creating a new record
use app models User ;
$ user = new User ();
$ user -> username = " jameshetfield " ;
$ user -> password = " 13831383 " ;
$ user -> city = " Downey " ;
$ user -> create (); //Saved into the database.
//or you can set the properties in the constructor
$ user = new User ([
' username ' => ' jameshetfield ' ,
' password ' => ' 13831383 ' ,
' city ' => ' Downey '
]);
$ user -> create ();ORM ใช้ตัวสร้างการสืบค้น DEJ ใต้เพื่อสร้างแบบสอบถามที่จำเป็น
โปรดทราบว่า create (), update () และ delete () ส่งคืนแถวที่ได้รับผลกระทบซึ่งคุณสามารถตรวจสอบเพื่อดูว่าการดำเนินการสำเร็จหรือไม่
//Finding a record by a field named 'id'
$ user = User:: findById ( 11 );
//changing it's properties
$ user -> password = " through_the_never " ;
$ user -> update (); //Updated in the database.
//deleting it.
$ user -> delete ();โปรดทราบว่า ฟังก์ชั่นเหล่านี้ทำงานกับคีย์หลักของบันทึก
//Finding records by some condition.
$ users = User:: find ()-> where ( ' city ' , ' = ' , ' Sari ' )-> getAll (); //Returns an array of User objects.
//A more complex one
$ users = User:: find ()-> where ( ' city ' , ' = ' , ' Sari ' )-> andWhere ( ' age ' , ' > ' , 20 )-> orderBy ( ' age ' , ' ASC ' )
-> limit ( 25 )
-> offset ( 100 )-> getAll ();
//Don't forget the get*() method!
//Retrieveing all records
$ users = User:: getAll (); //doesn't need a getAll() at the end because it knows what to do.
//The method for deleting by condition is named 'wipe'
$ users = User:: wipe ()-> where ( ' status ' , ' = ' , ' banned ' )-> orWhere ( ' email_confirmation ' , ' = ' , ' 0 ' )-> execute ();
//counting all records
$ userCount = User:: countAll ();
//counting records that have a certain condition
$ userCount = User:: count ()-> where ( ' city ' , ' = ' , ' Sari ' )-> getInt (); //getInt() returns the count of the results as an Integer. การตรวจสอบข้อมูลใน dejframework ได้รับการจัดการโดยคลาส dejValidator คุณสามารถใช้มันได้หลายวิธี:
App::Validator() ซึ่งให้อินสแตนซ์ของ Singleton มันยอมรับพารามิเตอร์ 2 พารามิเตอร์: ค่า ที่จะตรวจสอบ (สตริง, int, อาร์เรย์, วัตถุ) และชุดของ กฎ เพื่อตรวจสอบข้อมูลกับมาดูกันว่ามันทำงานอย่างไร:
use dej App ;
//Validate a single value according to a set of rules seperated by "|".
$ result = App:: Validator ()-> validate ( " This is going to be validated " , " required|string|min:5|max:30 " );
//Validate an object or and array:
$ result = App:: Validator ()-> validate ( $ user , [ ' username ' => ' required|string|email|min:5|max:30 ' ,
' password ' => ' required|string|min:10|max:100 ' ],
' age ' => ' int|min:18|max:99 ' ); วิธี validate() ส่งคืนอาร์เรย์ หากไม่มีข้อผิดพลาดการตรวจสอบและข้อมูลนั้นถูกต้องมันจะเป็นอาร์เรย์ที่ว่างเปล่า ดังนั้นคุณสามารถตรวจสอบได้ว่าข้อมูลนั้นถูกต้องด้วย empty() หรือไม่ หากมีข้อผิดพลาดการตรวจสอบใด ๆ คุณจะเห็นพวกเขาในอาร์เรย์
//The rules have changed
$ errors = App:: Validator ()-> validate ( " This is going to be validated " , " required|string|email|min:5|max:10 " );
var_dump ( $ errors );นี่คือสิ่งที่รหัสด้านบนจะส่งออก:
array
0 => 'This Field should be an email'
1 => 'This Field should be less than 10'
ตอนนี้มีวัตถุ:
$ errors = App:: Validator ()-> validate ( $ obj , [ ' email ' => ' required|string|email ' ,
' password ' => ' required|string ' ,
' age ' => ' int ' ]);
var_dump ( $ errors );เอาท์พุท:
array (size=3)
'email' =>
array (size=1)
0 => string 'This Field should be an email'
'password' =>
array (size=2)
0 => string 'This Field is Required'
1 => string 'This Field should be more than 10'
'age' =>
array (size=1)
0 => string 'This Field should be a number'
อย่างที่คุณเห็นหากคุณผ่านวัตถุหรืออาร์เรย์ไปยังตัวตรวจสอบความถูกต้องมันจะส่งคืนข้อผิดพลาดที่เกี่ยวข้องกับแต่ละฟิลด์ในอาร์เรย์เชื่อมโยงที่คีย์เป็นชื่อฟิลด์และค่าเป็นอาร์เรย์ที่มีข้อผิดพลาดที่เกี่ยวข้องกับฟิลด์นั้น
validate() บนวัตถุคำขอ $ errors = App:: Request ()-> validate ([ ' email ' => ' required|string|email ' ,
' password ' => ' required|string|min:10|max:100 ' ]);
var_dump ( $ errors ); เยี่ยมชม yoursite.local/?email=notanemail&password=123 จะส่งผลใน:
array (size=2)
'email' =>
array (size=1)
0 => string 'This Field should be an email'
'password' =>
array (size=1)
0 => string 'This Field should be more than 10'
/app/models/User.php : class User extends dej mvc Model
{
.
.
.
protected static $ validationRules = [ " username " => " required|string|min:5|max:20 " ,
" password " => " required|string|min:5|max:255 " ,
" city " => " string|max:10 " ];
.
.
.
}ตอนนี้คุณสามารถตรวจสอบอินสแตนซ์ของโมเดล:
$ user = User:: getById ( 11 );
$ errors = $ user -> validate (); //returns errors in array like the previous examples.
$ isValid = $ user -> isValid (); //returns true or falseเมื่อคุณมีข้อผิดพลาดคุณสามารถส่งผ่านไปยังมุมมองเพื่อแสดงหรือเปลี่ยนเส้นทางไปยังที่ใดที่หนึ่งกับพวกเขา:
//in the controller
$ errors = $ user -> validate ();
if (! empty ( $ errors )) return App:: Response ()-> redirect ( ' / ' )-> withErrors ( $ errors ); ข้อความการตรวจสอบความถูกต้องที่คุณเห็นในตัวอย่างก่อนหน้านี้คือข้อความเริ่มต้นจะเกิดอะไรขึ้นถ้าคุณต้องการเปลี่ยนแปลงหรือมีข้อความการตรวจสอบความถูกต้องในภาษาของคุณเอง? ดูที่ /app/locale/en/validation/messages.php :
return [
" required " => " This Field is Required " ,
" string " => " This Field should be an string " ,
" int " => " This Field should be a number " ,
" min " => " This Field should be more than %s " ,
" max " => " This Field should be less than %s " ,
" email " => " This Field should be an email " ,
]; คุณจะเห็นว่าคีย์สอดคล้องกับประเภทการตรวจสอบความถูกต้องและค่าเป็นข้อความการตรวจสอบความถูกต้อง ตัวแปรจะรวมอยู่ในข้อความด้วย 's' ใน /app/locale ที่คุณสามารถสร้างไดเรกทอรีของคุณเองสำหรับภาษาของตัวเลือกของคุณและในไดเรกทอรีนั้นมีข้อความการตรวจสอบความถูกต้องของคุณเองพร้อมตัวแปร
คุณสามารถตั้งค่าตำแหน่งเริ่มต้นใน /config.json
คุณสามารถเปลี่ยนได้ในแอพของคุณโดย App::Config()->locale = 'your_locale';
เพื่อให้ทำงานกับเซสชัน PHP ได้ DEJFRAMEWORK ได้จัดเตรียมคลาส dejSession นี่คือตัวอย่างบางส่วนเกี่ยวกับวิธีการใช้:
//to set session variables
App:: Session ()-> save ([
' key1 ' => ' value1 ' ,
' key2 ' => ' value2 '
]);
//to get session variables
$ value1 = App:: Session ()-> get ( ' key1 ' );
//to regenerate session id
App:: session ()-> regenerateId ();
//get all session variables as associative array
$ wholeSession = App:: session ()-> all ();
//destroy the session
App:: session ()-> destroy ();
//delete a variable
App:: session ()-> delete ( ' key1 ' );
//see if variable is set
$ trueOrFalse = App:: session ()-> isSaved ( ' key1 ' );หากคุณต้องการให้ตัวแปรเซสชันพร้อมใช้งานเฉพาะในคำขอถัดไปตัวอย่างเช่นข้อความแสดงข้อผิดพลาดบางอย่างคุณสามารถแฟลชไปยังเซสชันเช่นนี้:
App:: session ()-> flash ([ ' message ' => ' Registered Successfully! ' ]);และเรียกคืนในคำขอถัดไป:
$ message = App:: session ()-> getFlash ( ' message ' ); โปรดทราบว่าข้อความแฟลชมีเฉพาะในคำขอถัดไปและมีเฉพาะ ->getFlash()
คุณสามารถตั้งค่าเซสชันให้หมดอายุหลังจากเวลาที่กำหนดตั้งแต่การเข้าถึงเซสชันล่าสุด:
App:: Session ()-> save ([ ' key ' => ' value ' ]);
App:: Session ()-> lifeTime ( 60 ); //session will expire after 60 seconds since last access to the session. (last use of App::Session())บางครั้งคุณอาจต้องการให้แอปพลิเคชันของคุณจดจำอินสแตนซ์ของโมเดลสำหรับคำขอถัดไป ตัวอย่างเช่นคุณต้องการให้แอปคุณจดจำผู้ใช้ที่เข้าสู่ระบบหรือตะกร้าสินค้าที่ผู้ใช้ของคุณมีหรืออะไรก็ตาม หากคุณเก็บคีย์หลักของโมเดลที่คุณต้องการในเซสชันและเรียกใช้แบบสอบถามเพื่อเรียกคืนในทุกคำขอมันอาจน่าเบื่อ DeJframework แก้ปัญหานี้โดยให้ลักษณะที่คุณสามารถใช้ในโมเดลของคุณ ลองดู:
class ShoppingCart extends dej mvc Model
{
use dej traits IsStateful;
.
.
.
}ตอนนี้คุณมีวิธีการอีกสองสามวิธีในรุ่นของคุณ:
//suppose you want to create a shopping cart for a guest user and add a product to it:
$ cart = new ShoppingCart ();
$ cart -> addProduct ( 2389 );
$ cart -> create (); //stored in the db
//if you want to save it to the session get it easily in the next request
$ cart -> remember ( ' guest_shopping_cart ' ); //provide a key to remember it by this key.ตอนนี้ในคำขอถัดไป:
if (ShoppingCart:: isRemembered ( ' guest_shopping_cart ' )) //see if theres any cart in the session
$ cart = ShoppingCart:: retrieve ( ' users_shopping_cart ' ); //get's fresh data from the db
if (ShoppingCart:: hasChanged ( ' guest_shopping_cart ' ))
{
//checks if the cart in the database has changed **since you saved the cart in the session**.
doSomeThing ();
}
ShoppingCart:: forget ( ' guest_shopping_cart ' ); //forgets the cart. ในการนำเสนอข้อมูลของคุณต่อผู้ใช้คุณต้องมีส่วนต่อประสานกับผู้ใช้ ในเว็บส่วนใหญ่เวลานี้หมายถึงมาร์กอัป HTML ใน MVC ตรรกะจะต้องแยกออกจาก UI ดังนั้นคุณจึงใส่ HTML ของคุณในมุมมองและรวมถึงตรรกะการนำเสนอในพวกเขาเช่น echo ค่าหรือใส่อาร์เรย์ลงใน `` foreach`` เพื่อทำซ้ำ เพื่อสร้างมุมมองที่คุณควร:
/app/views มีมุมมองหนึ่งที่รวมอยู่ในค่าเริ่มต้น: index.phtml <!DOCTYPE html >
< html >
< head >
< title > < ?= $data- > user- > username ? > 's profile </ title >
</ head >
< body >
< h2 > View < ?= $data- > user- > username ? > 's Profile </ h2 >
< p > Username: < ?= $data- > user- > username ? > </ p >
< p > Password: < ?= $data- > user- > password ? > </ p >
< p > City: < ?= $data- > user- > city ? > </ p >
</ body >
</ html > class IndexController extends dej mvc Controller
{
public static function index()
{
$ user = User:: find ()-> where ( ' username ' , ' = ' , ' jameshetfield ' )-> getOne ();
return App:: View ( ' user ' , [ ' user ' => $ user ]); //the first argument is the view name,
the second one is the data you want to pass to
the view, in this example, a variable called ' user '
will be available in the user.phtml view
which contains the instance of user we fetched.
Piece of cake!
}
}คุณสามารถให้ข้อความแสดงข้อผิดพลาดได้เช่นกัน:
//in the controller
return App:: View ( ' user ' )-> withErrors ([ ' authorization ' => ' You are not allowed to view this user. ' ]);เฟรมเวิร์กจะทำให้มุมมองเป็นเอาต์พุตและผลลัพธ์จะเป็น:
<!DOCTYPE html >
< html >
< head >
< title > jameshetfield's profile </ title >
</ head >
< body >
<!-- access the errors like this: -->
< span class =" error " > < ?= $this- > errors('authorization') ? > </ span >
< h2 > View jameshetfield's Profile </ h2 >
< p > Username: jameshetfield </ p >
< p > Password: 13831383 </ p >
< p > City: Downey </ p >
</ body >
</ html >ตอนนี้เรามาดูตัวอย่างอื่นถ้าเราต้องการสร้างตารางของผู้ใช้ทั้งหมด แก้ไขมุมมองของคุณ:
<!DOCTYPE html >
< html >
< head >
< title > All Users </ title >
</ head >
< body >
< table >
< tr >
< th > username </ th >
< th > password </ th >
< th > city </ th >
</ tr >
< ?php foreach ($data- > users as $user): ? >
< tr >
< th > < ?= $user- > username ? > </ th >
< th > < ?= $user- > password ? > </ th >
< th > < ?= $user- > city ? > </ th >
</ tr >
< ?php endforeach; ? >
</ table >
</ body >
</ html >แล้วคอนโทรลเลอร์ของคุณ:
class IndexController extends dej mvc Controller
{
public static function index ()
{
$ users = User:: getAll ();
return App:: View ( ' user ' , [ ' users ' => $ users ]);
}
}และดูผลลัพธ์ด้วยตัวคุณเอง!
คุณสามารถสร้าง Partials View และวาง (รวม) ในมุมมองอื่น ๆ :
[/app/views/partials/header.phtml]
<!--begin header-->
<!DOCTYPE html >
< html >
< head >
< meta charset =" UTF-8 " >
< title > < ?= $data- > title ? > </ title >
</ head >
< body >
<!--end header--> และวางใน index.phtml :
[/app/views/index.phtml]
< ?php $this- > paste('partials/header'); ? >
< h2 > < ?= $data- > message ? > </ h2 >
.
.
.
</ html > การรับรองความถูกต้องเป็นเรื่องที่เกี่ยวข้องกับแอปพลิเคชัน มันอาจแตกต่างกันมากในทุกแอปพลิเคชันดังนั้น dejframework ไม่ได้นำไปใช้ในเฟรมเวิร์กของตัวเองแทนคุณจะได้รับแบบจำลองผู้ใช้พื้นฐานเส้นทางการรับรอง IsStateful ถูกต้องและตรรกะ $user->remember() คอนโทรลเลอร์ที่ทำให้การรับรองความถูกต้องเกิดขึ้น สิ่งเดียวที่คุณต้องทำคือตั้งค่า 'default_auth_model' ใน config.json เพื่อให้สามารถใช้ App::Request->user() เป็น ::retrieve() ผู้ใช้จากเซสชันโดยใช้ลักษณะ IsStateful
ลองดูที่คอนโทรลเลอร์โมเดลและมุมมองที่มีให้ในกรอบการทำงานเพื่อให้เข้าใจถึงการทำงานของมัน และเปลี่ยน/ปรับปรุง/ลบออกตามที่คุณต้องการ
หมายเหตุ: เพื่อให้โมเดลผู้ใช้ทำงานได้อย่างถูกต้องคุณต้องสร้างตารางชื่อ users ที่มีฟิลด์ 2:
TABLE: users
FIELD TYPE
id int, autoincrement
username varchar
password varchar(255) //password hashing system requires 255