Ejemplo n.º 1
0
 function __construct()
 {
     parent::__construct(array('@^admin\\b@' => function () {
         if (!$loggedIn) {
             header('Location: ' . Route::getFrontControllerPath() . '/login');
             exit;
         }
         // Return false, so the next route is also evaluated.
         return false;
     }, '@^admin$@' => 'showAdminIndex'));
 }
Ejemplo n.º 2
0
 protected function index()
 {
     die("<a href='" . Route::getFrontControllerPath() . "/customers'>Show customers</a><br>\n        <a href='" . Route::getFrontControllerPath() . "/customers/12'>Edit customer 12</a><br>\n        <form method='post' action='" . Route::getFrontControllerPath() . "/customers/12'><input type='submit' value='Update customer 12' /></form><br>\n        <a href='" . Route::getFrontControllerPath() . "/customers/12/orders'>Show orders from customer</a>");
 }
<?php

use PHPMICROLIB\Router\Route;
require_once Route::getFrontControllerDir() . '/models/customer_pdo.inc.php';
class CustomersController extends \PHPMICROLIB\Router\Controller
{
    private $pdoCustomer;
    function __construct()
    {
        parent::__construct(array('@^customers$@' => 'showCustomers', '@^customers/(?<id>\\d+)$@' => array('get' => 'editCustomer', 'post' => 'updateCustomer'), '@^customers/(?<id>\\d+)\\b@' => function () {
            self::handleRoute('CustomerOrdersController');
        }));
        $this->pdoCustomer = new PDOCustomer();
    }
    protected function showCustomers()
    {
        $customers = array_map(function ($customer) {
            return $customer->email;
        }, $this->pdoCustomer->getAll());
        die('Customers: ' . implode(', ', $customers));
    }
    protected function editCustomer($args)
    {
        die('Edit customer ' . $args['id']);
    }
    protected function updateCustomer($args)
    {
        die('Update customer ' . $args['id'] . ' from POST!');
    }
}
Ejemplo n.º 4
0
use PHPMICROLIB\Database\Crypt;
use PHPMICROLIB\Database\PDOModel;
use PHPMICROLIB\Router\Route;
// Set the connection parameters
PDOModel::setConnectionParameters('localhost', 'testuser', '', 'myapp');
// Reden om eerst te initializeren en dan pas handleRoute te doen, is
// dat je dan na initialisatie bijv. nog kunt kijken of je een andere
// directory op wil geven aan de handleroute methode. Bijv. als eerste
// deel van de path "api" is, dan altijd in de map "api" halen...
Route::initialize();
$pathParts = Route::getPathParts();
$isApi = !empty($pathParts) && $pathParts[0] === 'api';
try {
    if ($isApi) {
        Route::handleRoute('controllers/api');
    } else {
        Route::handleRoute('controllers');
    }
    throw new Exception("No controller found for this route!");
} catch (Exception $ex) {
    if ($isApi) {
        header('Content-Type:application/json');
        die(json_encode(array('error' => $ex->getMessage())));
    } else {
        $tpl = new Template('templates/app.tpl.php');
        $tpl->set('title', 'Error');
        $tpl->set('exception', $ex->getMessage());
        $tpl->set('content', '');
        die($tpl->parse());
    }
}
Ejemplo n.º 5
0
<?php

use PHPMICROLIB\Router\Route;
?>
<!doctype html>
<html>
  <head>
    <title><?php 
echo $this->esc($title);
?>
</title>
    <link rel="stylesheet" href="<?php 
echo Route::getFrontControllerPath();
?>
/css/app.css">
  </head>
  <body>
    <?php 
if (!empty($exception)) {
    ?>
    <div class="error"><?php 
    echo $this->esc($exception);
    ?>
</div>
    <?php 
}
?>
    <?php 
echo $content;
?>
  </body>
Ejemplo n.º 6
0
 protected function index()
 {
     die("<a href='" . Route::getFrontControllerPath() . "/about/12'>About...</a>");
 }
<?php

use PHPMICROLIB\Database\Crypt;
use PHPMICROLIB\Database\PDOModel;
use PHPMICROLIB\Router\Route;
require_once Route::getFrontControllerDir() . '/../src/crypt.inc.php';
// Properties moeten public zijn!
// Want de map functie zet deze.
// Ook mooier om hier aparte functies van te maken, zodat de Customer
// class lean and mean blijft. Deze maak je namelijk veel vaker en een DB
// class heb je maar weinig nodig. Hierdoor blijven de Customer e.d. classes
// klein en lichtgewicht.
// Nadeel is dat de foreignProperties e.d. niet direct in de class staan
// maar in de PDO class... maar eigenlijk is dat goed, want nu is class
// helemaal gescheiden van database class.
class Customer
{
    public $id;
    public $username;
    public $pwd;
    public $email;
    public $firstname;
    public $street;
}
class PDOCustomer extends PDOmodel
{
    protected $className = 'Customer';
    protected $foreignProperties = ['street'];
    protected $encryptedProperties = ['email'];
    protected $selectQuery = "SELECT c.*, a.street\n      FROM customer AS c\n      LEFT JOIN address AS a\n        ON a.customer_id = c.id";
    public function getAll()