This approach might be wrong or right, if you have something to add please correct me since i want to learn!
The past few weeks I'v been trying to build a MVC structure REST API in PHP.And this is my way of understanding how it works:
- So the User talks to the app thorough the Router.
- According to the Users request the router picks a controller if valid
- The controller picks the model and choses the right action to be done
- Than when the action is performed the model talks back to the controller
- The controller renders the output though the view
This approach might be wrong ,it's my opinion and my understanding though what I'v red
So the project it self
Since this will act as an API i have decided to put in a sub domain.For example api.domain.com
Here is the file & folder structure:
. ├── README.md ├── app │ └── MVCtest │ ├── controller │ │ └── test.php │ ├── core │ │ ├── conf │ │ │ └── db.con.yml │ │ ├── database │ │ │ ├── db.con.php │ │ │ └── db.handler.php │ │ ├── handler │ │ │ └── sql.error.php │ │ └── helper │ │ ├── jsonSerializer.php │ │ ├── model │ │ │ └── model.helper.php │ │ └── stdObject.php │ ├── model │ │ ├── Test │ │ │ ├── get.user.php │ │ │ ├── test.add.user.php │ │ │ └── update.user.php │ │ └── Test.php │ └── view │ └── api.json.php ├── composer.json ├── composer.lock ├── diagram_1.png └── public └── index.php
- The public directory is the door to the web
- The app/core holds some helpers(Json Serialization) and handlers(error handler).Also not to forget it holds the database configuration and an abstract class that will have use in the model.
- The view hold JSON encoding and it's the end point
- The model hold the functions and communicates to the database
- The controller hold the controller and it's an extension to the router.
The thing that concerns me the most is everything :P But I would like if someone would check out my model. So basically the main mode for example Test.php ,hold functions for certain actions.It extends those actions from Test/classes Each of those classes holds a function and extends an class.
Here is it:
Test.php
namespace MVCtest\model; use MVCtest\core\database\DbHandler as DbHandler; use MVCtest\model\Test\AddUser as AddUser; use MVCtest\model\Test\GetUser as GetUser; use MVCtest\model\Test\UpdateUser as UpdateUser; class Test{ //Add User public function add_user($userObject){ $add_user = new AddUser(); return $add_user->main($userObject); } //Get User public function get_users($userObjects = []){ $get_user = new GetUser(); return $get_user->main($userObject = []); } //Update User public function update_user($userObjects){ $update_user = new UpdateUser(); return $update_user->main($userObjects); } }
Here is one of the function classes:
namespace MVCtest\model\Test; use MVCtest\core\helper\model\ModelHelper as ModelHelper; class AddUser extends ModelHelper{ public function main($TestObject){ //Query $stmt = parent::$dbConn->prepare("INSERT INTO user(name,surname,age) VALUES(:name,:surname,:age)"); $stmt->bindParam(':name',$TestObject->name); $stmt->bindParam(':surname',$TestObject->surname); $stmt->bindParam(':age',$TestObject->age); return parent::query($stmt,NULL); } }
And here are the classes the function classes on the model extend
ModelHelper.php
use MVCtest\core\database\DbHandler as DbHandler; use MVCtest\core\handler\SqlError as SqlError; class ModelHelper extends DbHandler{ public $TestObject; private $status = 200; private static $row = NULL; public function __construct($test = []){ $this->TestObject = $test; parent::__construct(); } public function query($stmt,$row = []){ try{ $stmt->execute(); $row != NULL ? self::$row = call_user_func(array($this,'fetchAll'), $stmt) : self::$row = NULL; }catch(\PDOException $e){ $error = new SqlError($e->getCode()); $this->status = $error->determiner(); } return [$this->status,self::$row]; } //Fetch All private function fetchAll($stmt){ return $stmt->fetchAll(\PDO::FETCH_OBJ); } }
DbHandler.php
namespace MVCtest\core\database; use MVCtest\core\database\DbConn as DbConn; abstract class DbHandler { //Database Connection protected static $dbConn = null; public function __construct(){ $database = new DbConn; self::$dbConn = $database->setup(); } abstract function query($stmt,$row); }
Here is the complete project o github
I would appreciate if someone would give a quick view on what i have built to tell me if it's at least okay.And what should be changed.