Hexacore هو إطار PHP صغير يعتمد على نمط MVC. في البداية مشروع شخصي لتحسين PHP الخاص بي مع أنماط التصميم والميزات الجديدة من PHP 7.0+
أعتقد أن هذا المشروع يمكن أن يكون مفيدًا للشخص الذي يرغب في تعلم PHP ولديه فهم أفضل لنمط MVC.
للحصول على نشر أسهل ، يمكنك استخدام Docker و Docker-Corm:
بعد تثبيت كل من Docker و Docker-Corm ، تحتاج فقط إلى تنفيذ الأمر التالي في جذر المشروع
$ docker-compose upيجب أن يكون الموقع يعمل على http: // localhost.
يمكنك أيضًا استخدام هذا الإطار باستخدام WAMP/LAMP/XAMP
بعد إنشاء قاعدة بيانات مع MySQL ، يمكنك ربطها بالإطار مع ملف confictarty الموجود في التطبيق/config/database.json
{
"dbname" : " myDB " ,
"host" : " db " ,
"port" : 3306 ,
"user" : " hexacore " ,
"password" : " test "
}كما قلت في وقت سابق ، اتبع نمط 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 !!
بشكل افتراضي ، لا يلزم إجراء فهرس وعمل الفهرس (الوظيفة) في عنوان URL. يمكن بعد ذلك الوصول إلى الصفحة الرئيسية بواسطة كل من http: // localhost و http: // localhost/index/index. لذلك تشير السلسلة بعد الأول / في عنوان 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 ! " );
}
}يمكنك إضافة أكبر عدد ممكن من معلمة GET كما تريد.
يمكنك أيضًا الحصول على هذه المتغيرات بالطريقة القياسية ( 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 أيضا للالتقاط. يحتوي وحدة التحكم على طريقة deticated لتقديم العرض من ملفات 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.
كما أنني لست متأكدًا من أن Auth و Firewall سيكون قابلاً للاستخدام في المستقبل.