From c1e05818a88c76efc53d264c34d8147e4dab055a Mon Sep 17 00:00:00 2001 From: Alan Berdinelli Date: Sun, 16 Jul 2017 19:54:05 -0300 Subject: [PATCH] Added some features --- README.md | 17 +++++++--- index.php | 72 ++++++++++++++++++++++++++++++++++++++++ src/Express/Express.php | 46 +++++++++++++++++++++++++ src/Express/Response.php | 16 ++++++++- 4 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 index.php diff --git a/README.md b/README.md index a742939..98c652f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # ExpressPHP -This framework tries to clone the NodeJS [express framework](https://www.npmjs.com/package/express) functionality. +This framework tries to clone the NodeJS [ExpressJS framework](https://www.npmjs.com/package/express) functionality. ## Install **Note**: To run ExpressPHP you need PHP >= 7.0 and Apache. -The preferred installation is using Composer: +The preferred installation is using Composer: -`composer require aeberdinelli/express-php v1.0.1` +`composer require aeberdinelli/express-php v1.0.3` Then, move the .htaccess to the root of your site and you're done: @@ -43,7 +43,7 @@ $router->get('/', function($req, $res) { }); ``` -You can handle post requests as well using post() instead of get(). +You can handle post requests as well using post() instead of get(). Same for put() and delete(). ## Route with dynamic parameters You can route dynamic URL using parameters, for example: @@ -83,6 +83,8 @@ $router->post('/', function($req, $res) { }); ``` +**TIP**: There are a few more examples in the `index.php` file in this repository. + ## Static files If you wish to serve static files (likes images, html only) you can use: @@ -98,6 +100,9 @@ You have avaible [Pug](https://pugjs.org) (ex Jade) and [Mustache](https://musta // Configure the engine to Pug $express->set('view engine','pug'); +// Jade was renamed to Pug, but we recognize it ;) +$express->set('view engine','jade'); + // Or Mustache $express->set('view engine','mustache'); @@ -115,11 +120,13 @@ $router->get('/users/:username', function($req, $res) { 'name' => $req->params->username )); - // Now in the template, you can use #{name} to get that variable! + // Now in jade, you can use #{name} to get that variable! }); ``` ## Request info - You have the body of the request in $res->body no matter if you re handling POST or PUT. +- You have the query string under $req->query +- You have the cookies in $req->cookies - You have all the request headers in $req->headers diff --git a/index.php b/index.php new file mode 100644 index 0000000..f86f98b --- /dev/null +++ b/index.php @@ -0,0 +1,72 @@ +getInfo(); + +/** + * Here you have a few common usages for ExpressPHP + */ + +// Handle $_POST variables +$router->post('/', function($req, $res) { + $res->json(array( + 'name' => $req->body->name + )); +}); + +// Handle $_GET variables in /page path, for example /path?name=Alan +$router->get('/path', function($req, $res) { + $res->send('Hi, '.$req->query->name); +}); + +// Handle dynamic URL +$router->get('/:page', function($req, $res) { + $res->send('You are visiting '.$req->params->page.'!'); +}); + +// You can handle nested dynamic paths: +$router->get('/:author/:id', function($req, $res) { + $res->send('You are visiting the post id: '.$req->params->id.' by '.$req->params->author); +}); + +// You can even use regex +$router->get('/([0-9]{4})-word', function($req, $res) { + // For example, here we want 4 numbers and then "-word" +}); + +// You have a few useful helpers for cookies +$router->get('/cookie', function($req, $res) { + // Get a cookie named "username" + $name = $req->cookies->username; + + // Set a cookie + $res->cookie('cookieName', 'cookieContent'); + + // Remove a cookie + $res->clearCookie('username'); +}); + +// And for redirections, too +$router->get('/redirect', function($req, $res) { + // Using Location header + $res->location('/other-page'); + + // Or using a 302 redirect: + $res->redirect('/other-page'); + + // Or a 301 permanent redirect + $res->redirect('/other-page', true); +}); + +/** + * listen() must receive an instance of Router to work. + */ +$express->listen($router); +?> diff --git a/src/Express/Express.php b/src/Express/Express.php index b8d7dfd..119934e 100644 --- a/src/Express/Express.php +++ b/src/Express/Express.php @@ -51,6 +51,18 @@ class Express */ private $cookies; + /** + * The querystring of this request + * @var array + */ + private $query; + + /** + * Variables avaible within the entire instance and avaible in the template views + * @var stdClass + */ + public $locals; + /** * The default settings for ExpressPHP * @var array @@ -102,6 +114,13 @@ public function __construct() $this->method = $_SERVER['REQUEST_METHOD']; $this->headers = apache_request_headers(); $this->cookies = (object) $_COOKIE; + $this->locals = new \stdClass; + + // Get the querystring, remove the route from it + parse_str($_SERVER['QUERY_STRING'], $result); + unset($result['route']); + + $this->query = (object) $result; // Obtain the request body switch ($this->method) @@ -125,6 +144,32 @@ public function __construct() } } + /** + * Gets the collected info + * + * @param bool Return the results instead of dump + * @return mixed + */ + public function getInfo($return = false) + { + $info = array( + 'QueryString' => $_SERVER['QUERY_STRING'], + 'ParsedQueryString' => $this->query, + 'ParsedURL' => $this->current, + 'Headers' => $this->headers, + 'Cookies' => $this->cookies, + 'Body' => $this->body, + 'PHPVersion' => phpversion() + ); + + if ($return) + { + return $info; + } + + var_dump($info); + } + /** * An alias for $this->setting() */ @@ -237,6 +282,7 @@ public function listen($router, $callback = null) $request = (object) array( 'params' => (object) $variables, 'headers' => $this->headers, + 'query' => $this->query, 'cookies' => $this->cookies, 'body' => $this->body ); diff --git a/src/Express/Response.php b/src/Express/Response.php index 4776231..08edbc2 100644 --- a/src/Express/Response.php +++ b/src/Express/Response.php @@ -28,6 +28,12 @@ class Response */ private $cookies = false; + /** + * Variables avaible within the entire instance (@see \Express\Express) + * @var stdClass + */ + private $locals; + /** * An instance of the view engine */ @@ -37,13 +43,19 @@ class Response * Constructor * * @param array The default express settings + * @param stdClass An object with the app locals (@see \Express\Express) * @return void */ - public function __construct($settings = array()) + public function __construct($settings = array(), $locals = null) { $this->headers = array(); $this->settings = $settings; + if (!$locals) + { + $this->locals = new \stdClass; + } + if (in_array($this->settings['view_engine'], array('jade','pug'))) { $this->engine = new Jade($this->settings['cache_dir'], $this->settings['pretty_print']); @@ -202,7 +214,9 @@ public function render($path, $scope = array()) // A not so pretty little hack to send the variables to the view eval(' $scope = json_decode(\''.json_encode($scope).'\', true); + $locals = json_decode(\''.json_encode($this->locals).'\', true); extract($scope); + extract($locals); ?> '.$this->engine->render($view, $scope)); }