Hexacore เป็นเฟรมเวิร์ก PHP ขนาดเล็กตามรูปแบบ MVC เริ่มแรกโครงการส่วนบุคคลเพื่อปรับปรุง PHP ของฉันด้วยรูปแบบการออกแบบและคุณสมบัติใหม่จาก Php 7.0+
ฉันคิดว่าโครงการนี้มีประโยชน์สำหรับคนที่ต้องการเรียนรู้ PHP และมีความเข้าใจที่ดีขึ้นเกี่ยวกับรูปแบบ MVC
เพื่อการปรับใช้ที่ง่ายขึ้นคุณสามารถใช้ Docker และ Docker-compose:
หลังจากการติดตั้งทั้ง Docker และ Docker-compose คุณเพียงแค่ต้องดำเนินการคำสั่งต่อไปนี้ที่รูทของโครงการ
$ docker-compose upเว็บไซต์ควรทำงานบน http: // localhost
นอกจากนี้คุณยังสามารถใช้เฟรมเวิร์กนี้ด้วย WAMP/LAMP/XAMP
หลังจากสร้างฐานข้อมูลด้วย MySQL คุณสามารถเชื่อมโยงไปยังเฟรมเวิร์กด้วยไฟล์ conficutation ที่อยู่ในแอพ/กำหนดค่า/ฐานข้อมูล. json
{
"dbname" : " myDB " ,
"host" : " db " ,
"port" : 3306 ,
"user" : " hexacore " ,
"password" : " test "
}ดังที่ฉันได้กล่าวไว้ก่อนหน้านี้ Hexacore ทำตามรูปแบบ MVC หมายความว่าแอปพลิเคชันถูกแบ่งออกเป็น 3 ส่วน:
สิ่งแรกที่ต้องทำเพื่อแสดงหน้าแรกคือการสร้าง indexController
นี่เป็นวิธีที่ง่ายที่สุดในการทำเช่นนั้น:
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Response ResponseInterface ;
use Hexacore Core Response Response ;
class IndexController extends Controller
{
public function index (): ResponseInterface
{
return new Response ( " Hello world ! " );
}
} คอนโทรลเลอร์ต้องขยายจากคลาส Controller !!
โดยเริ่มต้น indexcontroller และการดำเนินการดัชนี (ฟังก์ชั่น) ไม่จำเป็นใน URL โฮมเพจสามารถเข้าถึงได้โดยทั้ง http: // localhost และ http: // localhost/index/index ดังนั้นสตริงหลังจาก URL แรก / ใน URL อ้างถึงคอนโทรลเลอร์และสตริงอื่น ๆ การกระทำ
งานเฟรมช่วยให้คุณสามารถแทรกตัวแปรใน URL avec ตัวควบคุมและการกระทำดังที่แสดงไว้ที่นี่:
http://localhost/user/get/athena
จากนั้นคุณสามารถจัดการตัวแปรโดยตรงเป็นพารามิเตอร์ของการกระทำของคุณได้รับคลาส UserController :
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Response ResponseInterface ;
use Hexacore Core Response Response ;
class UserController extends Controller
{
public function get ( String $ name ): ResponseInterface
{
return new Response ( " Welcome $ name ! " );
}
}คุณสามารถเพิ่มพารามิเตอร์ได้มากเท่าที่คุณต้องการ
นอกจากนี้คุณยังสามารถรับตัวแปรเหล่านี้ด้วยวิธีมาตรฐาน ( http://localhost/user/get?name=athena ) ด้วยวัตถุคำขอ:
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Response ResponseInterface ;
use Hexacore Core Response Response ;
class UserController extends Controller
{
public function get (): ResponseInterface
{
//for a post variable submitted
//$this->request->getPost("var");
$ name = $ this -> request -> getQuery ( " var " );
return new Response ( " Welcome $ name ! " );
}
}Hexacore ใช้ PHP สำหรับเทมเพลต คอนโทรลเลอร์มีวิธีการที่ตรวจสอบแล้วเพื่อแสดงมุมมองจากไฟล์ PHP
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Response Response ;
class UserController extends Controller
{
public function index (): Response
{
return $ this -> render ( " user/index.php " );
}
} รหัสนี้จะแสดงไฟล์ index.php โดยตรงในไฟล์เทมเพลต base.php คุณสามารถแก้ไขไฟล์เทมเพลตใน App/src/view/base/php
ในฐานะนักพัฒนาคุณเพียงแค่ต้องระบุเส้นทางของไฟล์ของคุณ (ดังที่แสดง user/index.php ) แต่ยังต้องวางไว้ในไฟล์ base.php
ตัวอย่างเช่น:
...
< main >
< ?php echo $block1; ? >
</ main >
...คุณสามารถใช้ Hexacore เพื่อคงข้อมูลไว้ โดยค่าเริ่มต้นจะใช้ฐานข้อมูล MySQL
คุณสามารถกำหนดค่าชื่อฐานข้อมูลและการเข้าถึงใน App/config/database.json
{
"dbname" : " myHexacoreDb " ,
"host" : " db " ,
"port" : 3306 ,
"user" : " Athena " ,
"password" : " myDatabasePassword "
}ข้อมูลจากฐานข้อมูลสามารถเรียกคืนได้ในคอนโทรลเลอร์:
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Model Repository ModelRepository ;
use Hexacore Core Response ResponseInterface ;
use App Model User ;
class UserController extends Controller
{
public function index ( ModelRepository $ modelRepository ): ResponseInterface
{
// to get all users
$ allUsers = $ modelRepository -> setModel (User::class)-> findAll ();
// to get the user with the id 1
$ user = $ modelRepository -> findById ( 1 );
return $ this -> render ( " user/index.php " , [
" users " => $ allUsers ,
" user " => $ user
]);
}
} คลาส ModelRepository จะส่งคืนวัตถุโมเดลในตัวอย่างนี้โมเดลผู้ใช้
เพื่อให้สามารถรับข้อมูลผู้ใช้ทั้งหมดที่คุณต้องสร้างแบบจำลองก่อน แบบจำลองจะต้องอธิบายข้อมูลที่คุณต้องการจัดเก็บโดยใช้คุณสมบัติส่วนตัว โมเดลเป็นคลาสที่ใช้ ManageableModelInterface สิ่งนี้อนุญาตให้ Hexacore โต้ตอบกับฐานข้อมูลของเขาผ่านรุ่นนี้และจัดการกับวัตถุที่สร้างไว้ล่วงหน้าเช่น ModelManager ได้อย่างง่ายดาย
ตัวอย่าง :
<?php
namespace App Model ;
use Hexacore Core Model ManageableModelInterface ;
class User implements ManageableModelInterface
{
private $ id ;
private $ name ;
public function getId () : ? int
{
return $ this -> id ;
}
public function setId ( int $ id ) : ManageableModelInterface
{
$ this -> id = $ id ;
return $ this ;
}
} คุณต้องสร้างตารางที่เกี่ยวข้องในฐานข้อมูล MySQL ของคุณด้วยฟิลด์เดียวกัน (ที่นี่ id และ name ) และ User ชื่อตาราง
เราเห็นก่อนหน้านี้ว่า ModelRepository สามารถใช้เพื่อดึงข้อมูลจากฐานข้อมูล ตอนนี้เพื่อสร้างและอัปเดตโมเดลเราสามารถใช้ ModelManager
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Model Manager ModelManager ;
use Hexacore Core Response ResponseInterface ;
use Hexacore Core Response Redirect RedirectionResponse ;
use Hexacore Helpers Url ;
use App Model User ;
class UserController extends Controller
{
public function create ( ModelManager $ modelManager , string $ name , Url $ url ): ResponseInterface
{
// create a new user with a personal name
$ user = new User ();
$ user -> setName ( $ name );
// actually insert the new user in the database
$ modelManager -> persist ( $ user );
// redirection to another page
return new RedirectionResponse ( $ url -> baseUrl ( " user " ));
}
} ด้วย ModelManager คุณสามารถลบหรืออัปเดตโมเดลได้
<?php
namespace App Controller ;
use Hexacore Core Controller ;
use Hexacore Core Model Manager ModelManager ;
use Hexacore Core Response ResponseInterface ;
use App Model User ;
class UserController extends Controller
{
/**
* For a resource like : user/update/{id}/{name}
* Example of the query :
* http://www.yourwebsite.com/user/update/1/zeus
*/
public function update ( ModelManager $ modelManager , User $ user , string $ name ): ResponseInterface
{
// the $user value will be a User object with data from the user number id
// we change the user name
$ user -> setName ( $ name );
// we persist this change in the database
$ modelManager -> persist ( $ user );
return new Response ( " User updated " );
}
/**
* For a resource like : user/delete/{id}
* Example of the query :
* http://www.yourwebsite.com/user/delete/1
*/
public function delete ( ModelManager $ modelManager , User $ user ): ResponseInterface
{
// again $user will be a User Model corresponding to the user with
// the id, 1 in the example.
// delete a specific user in the database
$ modelManager -> delete ( $ user );
return new Response ( " User deleted " );
}
}โครงการนี้ได้รับการพัฒนาเพื่อความสนุกสนานและอาจไม่ได้รับการปรับปรุง แต่ฉันเห็นห้องพักมากมายสำหรับการปรับปรุง:
สำหรับอนาคตการทดสอบเพิ่มเติมเกี่ยวกับสภาพแวดล้อมหลายอย่างเป็นสิ่งจำเป็นเช่นบนเว็บเซิร์ฟเวอร์ Nginx
นอกจากนี้ฉันไม่แน่ใจว่าการรับรองความถูกต้องและไฟร์วอลล์จะสามารถใช้งานได้ในอนาคต