헥사 코어는 MVC 패턴을 기반으로하는 작은 PHP 프레임 워크입니다. 처음에는 디자인 패턴으로 PHP를 개선하기위한 개인 프로젝트와 PHP 7.0+의 새로운 기능
이 프로젝트는 PHP를 배우고 싶어하는 사람에게 유용 할 수 있고 MVC 패턴을 더 잘 이해할 수 있다고 생각합니다.
더 쉬운 배포를 위해 Docker 및 Docker-Compose를 사용할 수 있습니다.
Docker와 Docker-Compose를 모두 설치 한 후 프로젝트의 루트에서 다음 명령을 실행하면됩니다.
$ docker-compose up그런 다음 웹 사이트는 http : // localhost에서 실행해야합니다.
이 프레임 워크를 WAMP/램프/XAMP와 함께 사용할 수도 있습니다
MySQL로 데이터베이스를 작성한 후 app/config/database.json에있는 Conficutation 파일로 프레임 워크에 연결할 수 있습니다.
{
"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 클래스에서 확장해야합니다 !!
기본적으로 IndexController 및 Index Action (함수)은 URL에서 필요하지 않습니다. 그런 다음 홈페이지는 http : // localhost 및 http : // localhost/index/index에 의해 도달 할 수 있습니다. 따라서 URL의 첫 번째 / 이후의 문자열은 컨트롤러를, 다른 문자열은 동작을 나타냅니다.
프레임 작업을 통해 URL AVEC에 가변을 얻을 수 있습니다.
http://localhost/user/get/athena
그런 다음 변수를 UserController 클래스의 액션 get 매개 변수로 직접 처리 할 수 있습니다.
<?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 구현하는 클래스입니다. 이를 통해 Hexacore는이 모델을 통해 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 웹 서버에서 여러 환경에 대한 더 많은 테스트가 필요합니다.
또한 Auth와 방화벽이 앞으로 사용할 수 있는지 확신 할 수 없습니다.