ヘキサコアは、MVCパターンに基づいた小さなPHPフレームワークです。最初は、PHP 7.0+のデザインパターンと新機能を使用してPHPを改善するための個人的なプロジェクト
このプロジェクトは、PHPを学び、MVCパターンをよりよく理解したい人に役立つと思います。
展開を容易にするために、DockerとDocker-Composeを使用できます。
DockerとDocker-Composeの両方をインストールした後、プロジェクトのルートで次のコマンドを実行する必要があります
$ docker-compose upその後、ウェブサイトはhttp:// localhostで実行されます。
このフレームワークをWamp/Lamp/Xampで使用することもできます
MySQLを使用してデータベースを作成した後、App/config/database.jsonにあるconticutationファイルを使用してフレームワークにリンクできます。
{
"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の両方でリーチできます。したがって、最初の / inの後の文字列は、コントローラーを参照し、他の文字列はアクションを参照します。
フレームワークを使用すると、ここに示すように、URL AVECにGET変数を挿入できます。
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 ! " );
}
}ヘキサコアはテンプレートにも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 " );
}
}このコードは、 base.phpテンプレートファイルにindex.phpファイルを直接レンダリングします。 App/src/view/base/phpでテンプレートファイルを変更できます。
開発者として、ファイルのパス(表示user/index.phpているように)のパスを指定するだけでなく、 base.phpファイルに置く場所も指定する必要があります。
例えば:
...
< main >
< ?php echo $block1; ? >
</ main >
...ヘキサコアを使用してデータを持続できます。デフォルトでは、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を実装するクラスです。これにより、ヘキサコアはこのモデルを介してHEデータベースと対話し、 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 ;
}
} 同じフィールド(ここではidとname )とテーブル名Userを使用して、MySQLデータベースに対応するテーブルを作成する必要があります。
以前に、 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 Webサーバーでは、いくつかの環境でより多くのテストが必要です。
また、AuthとFirewallが将来使用できるかどうかはわかりません。