Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Added some features
Browse files Browse the repository at this point in the history
  • Loading branch information
aeberdinelli committed Jul 16, 2017
1 parent c43eff0 commit c1e0581
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
17 changes: 12 additions & 5 deletions 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:

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:

Expand All @@ -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');
Expand All @@ -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
72 changes: 72 additions & 0 deletions index.php
@@ -0,0 +1,72 @@
<?php
include __DIR__.'/vendor/autoload.php';

use \Express\Express;
use \Express\Router;

$express = new Express();
$router = new Router();

// The complete request info can be var_dumped calling getInfo():
$express->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);
?>
46 changes: 46 additions & 0 deletions src/Express/Express.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
*/
Expand Down Expand Up @@ -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
);
Expand Down
16 changes: 15 additions & 1 deletion src/Express/Response.php
Expand Up @@ -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
*/
Expand All @@ -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']);
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit c1e0581

Please sign in to comment.