PHP code example of aeberdinelli / express-php

1. Go to this page and download the library: Download aeberdinelli/express-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aeberdinelli / express-php example snippets



Express\Express;
use Express\Router;

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

$router->get('/', function($req, $res) {
	$res->send('hello world!');
});

$express->listen($router);

$router = new Router();
$router->get('/', function($req, $res) {
    // This will be called when someone goes to the main page using GET method.
});

$router = new Router();
$router->get('/:something/:else', function($req, $res) {
    /**
     * Now let's imagine someone enters to URL: /hello/bye, then:
     *
     * $req->params->something will contain 'hello'
     * $req->params->else will contain 'bye'
     */
});

$router->post('/', function($req, $res) {
	$res->json(array(
		'error'		=> false,
		'message'	=> 'Hello'
	));
});

$router->post('/', function($req, $res) {
	$res->status(201)->json({
		'error'		=> false,
		'message'	=> 'Created!'
	});
});

// If you visit /static/image.png, this will return the file views/public/image.png
$router->use('/static', $express->static('views/public'));

// 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');

// Set the path to the template files
$express->set('views','./views/pug');

// Now you can do something like this
$router->get('/', function($req, $res) {
	$res->render('index.jade');
});

// Or this
$router->get('/users/:username', function($req, $res) {
	$res->render('index.jade', array(
		'name'	=> $req->params->username
	));

	// Now in jade, you can use #{name} to get that variable!
});


use \Express\ExpressLess;

/**
 * Let's say you have a /less folder on your project
 * And you want every request that goes into /css to load the less file within that folder instead
 *
 * In this example /css/global.css will load the compiled version of /less/global.less
 * Same for /css/something.css -> /less/something.less
 */

$less = new ExpressLess($express, array(
	'source'	=> __DIR__.'/less',
	'dest'		=> '/css'
));

// Yes, it's that simple.