@@ -82,3 +82,74 @@ PHP Core Framework is a simple and easy to use MVC framework for web application
8282 );
8383 ?>
8484 ```
85+
86+ ## Web API
87+ 1. Download, copy PHP Core framework to your project folder. Configure your web server
88+ 2. Create a model
89+ * Create "models" folder in your project folder
90+ * Create "info.php" in "models" folder
91+ ```php
92+ <?php
93+ class Info {
94+ public $Name = "PHP Core";
95+ public $Author = "Hung Thanh Nguyen";
96+ }
97+ ?>
98+ ```
99+ * Declare new model in module by modifying file "modules/model.php"
100+ ```php
101+ <?php
102+ // PHP Core
103+ // Author: Hung Thanh Nguyen
104+
105+ // define folder for all of models, the name ModelFolder should not be changed
106+ define("ModelFolder", "models/", true);
107+
108+ // decalre models here
109+ $ModelList = array (
110+ "info" => array ("file" => "info.php")
111+ );
112+ ?>
113+ ```
114+ 3. Create an API controller
115+ * Create "controllers" folder in your project folder
116+ * Create "api" folder in "controllers" folder
117+ * Create "getinfo.php" in "api" folder
118+ ```php
119+ <?php
120+ require_once($Models->GetModule("info"));
121+ class GetInfoController extends ApiController {
122+ public function Process() {
123+ if (preg_match("/^GET$/", $this->Request->Method)) {
124+ $this->Get();
125+ }
126+ else if (!preg_match("/^OPTIONS$/", $this->Request->Method)) {
127+ HttpCodes::MethodNotAllowed();
128+ }
129+ }
130+
131+ public function Get() {
132+ header("Content-Type: application/json");
133+ $response = new Info();
134+ echo(json_encode($response));
135+ }
136+ }
137+ ?>
138+ ```
139+ * Declare new API controller in module by modifying file "modules/api.php"
140+ ```php
141+ <?php
142+ <?php
143+ // PHP Core
144+ // Author: Hung Thanh Nguyen
145+
146+ // define folder for all of api controllers, the name ApiFolder should not be changed
147+ define("ApiFolder", "controllers/api/", true);
148+
149+ // declare api controllers here
150+ $ApiList = array (
151+ "getinfo" => array ("file" => "getinfo.php", "class" => "GetInfoController")
152+ );
153+ ?>
154+ ```
155+ 4. Retrieve data from new Web API by url: <Your project URL>/api/getinfo
0 commit comments