diff --git a/MVC.md b/MVC.md index e9070dcd3181f65a371de147c1cd4da1966828d9..2a07d03d0cfce97e43fd831f9d7a6c8959ca430b 100644 --- a/MVC.md +++ b/MVC.md @@ -1,144 +1,144 @@ -# Routing -To add new routing, add new entry to the variable $route in router.php with the format of:<br> -``` -REGEX => [ - METHOD => [ - "controller" => CONTROLLER_CLASS, - "views" => [VIEW1, VIEW2, ...] - ] -] -``` -e.g. -``` -"^tes$" => [ - "GET" => [ - "controller" => "TestController", - "views" => ["dump.php"] - ] -], -"^ajax\/tes$" => [ - "GET" => [ - "controller" => "TestController", - "views" => ["json.php"] - ] -], -"^static$" => [ - "GET" => [ - "views" => ["static.php"] - ] -], -``` -If controller is ommited, only the view will be included. - -Alternatively you can call addRoute in routing.php. -``` -addRoute("^tes$", "GET", ["json.php"], "TestController"); -``` - -# Models - -To create new model, use the namespace JLAS\Book\Model and extends the base class BaseModel. -By Overriding the BaseModel, you are required to implement 5 of these methods: findByID, find, create, update and delete. These are patterns for ORM, [read more](https://en.wikipedia.org/wiki/Object-relational_mapping). - -## Entity -Every model is accompanied by an Entity class. To create your own, extends the class BaseEntity and implements load and asArray. Unless needed, GenericEntity is enough of a representation of an Entity. - -## Querying -``` -$model = new Model\AccountModel(); - -Example with find: -$entity = $model->find("`username` = :id", array(":id"=>$username)); - -With conditional selector: -$entity = $model->where()->field(username)->equals($username)->finish(); --- OR -- -$entity = $model->where()->username->eq($username)->finish; - -``` -For conditional selector, see framework/lib/conditional.php@Conditional. - -# Controllers -To create new controller, use the namespace JLAS\Book\Controller and extends the base class BaseController. Then override the method 'run' which returns the data needed. Also use the method setResponse to set the response status of the controller. - -``` -namespace JLAS\Book\Controller; - -class CustomController extends BaseController { - - public function run($params) { - $data = ... // do your stuff here. - $this->setResponse(200); - return $data; - } - -} -``` - -Returning status code -``` -namespace JLAS\Book\Controller; - -class CustomController extends BaseController { - - public function run($params) { - $this->setResponse(400); - $this->setResponse(400, "missing parameter"); - return; - } - -} -``` - -Authentication -``` -namespace JLAS\Book\Controller; - -class CustomController extends BaseController { - - public function run($params) { - if ($this->isAuthenticated()) { - $this->setResponse(200); - return "Hello " . $this->getUsername(); - } - $this->setResponse(401, "Not authenticated"); - return; - } - -} -``` - -Arguments -``` -namespace JLAS\Book\Controller; - -class CustomController extends BaseController { - - public function run($params) { - if ($this->useArgs('username', 'target', 'id')) { - $this->setResponse(200); - // do your stuff here - return "Hello there {$this->getArg('target'), I'm {$this->getArg('username')}." - } - $this->setResponse(400); - // Appropriate message about missing argument is set automatically. - return; - } - -} -``` - -For available property and functions see controller/BaseController.php@BaseController - -# Views - -View are just plain .php file. Three built-in variables are supplied for the views if any controller was attached to it. - -## Built-in Variables - -| Name | Description | -|-|-| -| $path | The path of current request. | -| $data | Data retrieved from the controller, null if no controller is attached | -| $response | The response status from the controller, null if no controller is attached | -| $controller | The name of the attached controller | +# Routing +To add new routing, add new entry to the variable $route in router.php with the format of:<br> +``` +REGEX => [ + METHOD => [ + "controller" => CONTROLLER_CLASS, + "views" => [VIEW1, VIEW2, ...] + ] +] +``` +e.g. +``` +"^tes$" => [ + "GET" => [ + "controller" => "TestController", + "views" => ["dump.php"] + ] +], +"^ajax\/tes$" => [ + "GET" => [ + "controller" => "TestController", + "views" => ["json.php"] + ] +], +"^static$" => [ + "GET" => [ + "views" => ["static.php"] + ] +], +``` +If controller is ommited, only the view will be included. + +Alternatively you can call addRoute in routing.php. +``` +addRoute("^tes$", "GET", ["json.php"], "TestController"); +``` + +# Models + +To create new model, use the namespace JLAS\Book\Model and extends the base class BaseModel. +By Overriding the BaseModel, you are required to implement 5 of these methods: findByID, find, create, update and delete. These are patterns for ORM, [read more](https://en.wikipedia.org/wiki/Object-relational_mapping). + +## Entity +Every model is accompanied by an Entity class. To create your own, extends the class BaseEntity and implements load and asArray. Unless needed, GenericEntity is enough of a representation of an Entity. + +## Querying +``` +$model = new Model\AccountModel(); + +Example with find: +$entity = $model->find("`username` = :id", array(":id"=>$username)); + +With conditional selector: +$entity = $model->where()->field(username)->equals($username)->finish(); +-- OR -- +$entity = $model->where()->username->eq($username)->finish; + +``` +For conditional selector, see framework/lib/conditional.php@Conditional. + +# Controllers +To create new controller, use the namespace JLAS\Book\Controller and extends the base class BaseController. Then override the method 'run' which returns the data needed. Also use the method setResponse to set the response status of the controller. + +``` +namespace JLAS\Book\Controller; + +class CustomController extends BaseController { + + public function run($params) { + $data = ... // do your stuff here. + $this->setResponse(200); + return $data; + } + +} +``` + +Returning status code +``` +namespace JLAS\Book\Controller; + +class CustomController extends BaseController { + + public function run($params) { + $this->setResponse(400); + $this->setResponse(400, "missing parameter"); + return; + } + +} +``` + +Authentication +``` +namespace JLAS\Book\Controller; + +class CustomController extends BaseController { + + public function run($params) { + if ($this->isAuthenticated()) { + $this->setResponse(200); + return "Hello " . $this->getUsername(); + } + $this->setResponse(401, "Not authenticated"); + return; + } + +} +``` + +Arguments +``` +namespace JLAS\Book\Controller; + +class CustomController extends BaseController { + + public function run($params) { + if ($this->useArgs('username', 'target', 'id')) { + $this->setResponse(200); + // do your stuff here + return "Hello there {$this->getArg('target'), I'm {$this->getArg('username')}." + } + $this->setResponse(400); + // Appropriate message about missing argument is set automatically. + return; + } + +} +``` + +For available property and functions see controller/BaseController.php@BaseController + +# Views + +View are just plain .php file. Three built-in variables are supplied for the views if any controller was attached to it. + +## Built-in Variables + +| Name | Description | +|-|-| +| $path | The path of current request. | +| $data | Data retrieved from the controller, null if no controller is attached | +| $response | The response status from the controller, null if no controller is attached | +| $controller | The name of the attached controller | diff --git a/model/TokenModel.php b/model/TokenModel.php index 4e70cb0479c6e17b98455ecd8eed3f826c4ac036..fc666ac1dcbf1191a0fdbd659e9d872a919b72cd 100644 --- a/model/TokenModel.php +++ b/model/TokenModel.php @@ -1,94 +1,94 @@ -<?php -namespace JLAS\Book\Model; - -class TokenModel extends BaseModel { - - function __construct() { - parent::__construct("TokenEntity"); - $this->table = "token"; - } - - /** - * Find an account with specific username. - * @param string $id The username of the account. - * @return TokenEntity the entity with matching id. - */ - public function findByID($id) { - $result = $this->query("SELECT * FROM $this->table WHERE `username` = :id LIMIT 1", array(":id"=>$id)); - if (count($this->queryResult) > 0) { - return $this->newEntity($this->queryResult[0]); - } - return null; - } - - /** - * Find an entity matching the criteria. - * @return array an array containing all matching entities. - */ - public function find($criteria, $data) { - $result = $this->query("SELECT * FROM $this->table WHERE $criteria", $data); - $retval = array(); - if ($result) { - foreach ($this->queryResult as $row) { - array_push($retval, $this->newEntity($row)); - } - } - return $retval; - } - - /** - * Insert a new entity into the database. - * @param TokenEntity $entity the entity to be created. - * @return boolean if the creation is successful. - */ - public function create($entity) { - if ($this->findByID($entity->username) != null) { - return false; - } - $result = $this->query( - "INSERT INTO $this->table (`username`, `access-token`, `expiry`) VALUES (:username, :token, :expiry)", - array( - ":username" => $entity->username, - ":token" => $entity->token, - ":expiry" => $entity->expiry, - ) - ); - return $result; - } - - /** - * Update an entity in the database. - * @param TokenEntity $entity the entity to be updated. - * @return boolean if the update is successful. - */ - public function update($entity) { - $result = $this->query( - "UPDATE $this->table SET `access-token`=:token, `expiry`=:expiry WHERE `username`=:username", - array( - ":username" => $entity->username, - ":token" => $entity->token, - ":expiry" => $entity->expiry, - ) - ); - return $result; - } - - /** - * Delete an entity from the database. - * @param TokenEntity $entity the entity to be deleted. - * @return boolean if the deletion is successful. - */ - public function delete($entity) { - $result = $this->query( - "DELETE FROM $this->table WHERE `username`=:username", - array( - ":username" => $entity->username, - ) - ); - return $result; - - } - -} - +<?php +namespace JLAS\Book\Model; + +class TokenModel extends BaseModel { + + function __construct() { + parent::__construct("TokenEntity"); + $this->table = "token"; + } + + /** + * Find an account with specific username. + * @param string $id The username of the account. + * @return TokenEntity the entity with matching id. + */ + public function findByID($id) { + $result = $this->query("SELECT * FROM $this->table WHERE `username` = :id LIMIT 1", array(":id"=>$id)); + if (count($this->queryResult) > 0) { + return $this->newEntity($this->queryResult[0]); + } + return null; + } + + /** + * Find an entity matching the criteria. + * @return array an array containing all matching entities. + */ + public function find($criteria, $data) { + $result = $this->query("SELECT * FROM $this->table WHERE $criteria", $data); + $retval = array(); + if ($result) { + foreach ($this->queryResult as $row) { + array_push($retval, $this->newEntity($row)); + } + } + return $retval; + } + + /** + * Insert a new entity into the database. + * @param TokenEntity $entity the entity to be created. + * @return boolean if the creation is successful. + */ + public function create($entity) { + if ($this->findByID($entity->username) != null) { + return false; + } + $result = $this->query( + "INSERT INTO $this->table (`username`, `access-token`, `expiry`) VALUES (:username, :token, :expiry)", + array( + ":username" => $entity->username, + ":token" => $entity->token, + ":expiry" => $entity->expiry, + ) + ); + return $result; + } + + /** + * Update an entity in the database. + * @param TokenEntity $entity the entity to be updated. + * @return boolean if the update is successful. + */ + public function update($entity) { + $result = $this->query( + "UPDATE $this->table SET `access-token`=:token, `expiry`=:expiry WHERE `username`=:username", + array( + ":username" => $entity->username, + ":token" => $entity->token, + ":expiry" => $entity->expiry, + ) + ); + return $result; + } + + /** + * Delete an entity from the database. + * @param TokenEntity $entity the entity to be deleted. + * @return boolean if the deletion is successful. + */ + public function delete($entity) { + $result = $this->query( + "DELETE FROM $this->table WHERE `username`=:username", + array( + ":username" => $entity->username, + ) + ); + return $result; + + } + +} + ?> \ No newline at end of file diff --git a/view/error.php b/view/error.php index 7e1157ab87b33d0097af487910770541d62b6c69..b3bbd461de1e127454c162e69a6070af0d6b833e 100644 --- a/view/error.php +++ b/view/error.php @@ -1,16 +1,16 @@ -<!DOCTYPE html> -<html> -<head> - <meta charset="utf-8" /> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <title>Error <?php echo http_response_code() ?></title> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <!-- <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> --> - <!-- <script src="main.js"></script> --> -</head> -<body> - Error <?php echo http_response_code() ?> - <br> - <?php echo $_SERVER['REQUEST_URI']?> -</body> +<!DOCTYPE html> +<html> +<head> + <meta charset="utf-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <title>Error <?php echo http_response_code() ?></title> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <!-- <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> --> + <!-- <script src="main.js"></script> --> +</head> +<body> + Error <?php echo http_response_code() ?> + <br> + <?php echo $_SERVER['REQUEST_URI']?> +</body> </html> \ No newline at end of file