Пример #1
0
<?php

use Route69\Route;
use Route69\Route69;
require_once '../Route69.php';
$app = new Route69();
$app->config(function (Route $route) {
    $route->when('/test', ['controller' => 'test']);
});
$app->controller('test', function () {
    echo '<h1>Welcome</h1>';
});
Пример #2
0
<?php

use Route69\Route;
use Route69\Route69;
require_once '../Route69.php';
$app = new Route69();
/**
 * Routes can be defined two ways, they can be defined directly in the config
 * as a callback, but when you do that then that is only route can access that
 * controller (see the route: '/private').
 *
 * If the controller is defined using a string, than any route can use that
 * controller and the controller is considered public
 * (see the two routes: '/public' and '/i/am/public/too').
 */
$app->config(function (Route $route) {
    $route->when('/public', ['controller' => 'public'])->when('/private', ['controller' => function () {
        echo '<h1>I am a private controller</h1>';
    }])->when('/i/am/public/too', ['controller' => 'public']);
});
// example route: /public or /i/am/public/too
$app->controller('public', function () {
    echo '<h1>I am a public controller</h1>';
});
Пример #3
0
<?php

use Route69\Route;
use Route69\Route69;
use Route69\RouteParams;
require_once '../Route69.php';
$app = new Route69();
$app->config(function (Route $route) {
    $route->when('/age/#id', ['controller' => 'age'])->when('/username/:username', ['controller' => 'name'])->when('/color/@color', ['controller' => 'color']);
});
// example route: /age/15
$app->controller('age', function (RouteParams $routeParams) {
    echo '<h1>Your age is: ' . $routeParams->id . '</h1>';
});
// example route: /username/billy123
$app->controller('username', function (RouteParams $routeParams) {
    echo '<h1>Your username is: ' . $routeParams->username . '</h1>';
});
// example route: /color/red
$app->controller('color', function (RouteParams $routeParams) {
    echo '<h1>Your color is: ' . $routeParams->color . '</h1>';
});
Пример #4
0
<?php

use Route69\Route;
use Route69\Route69;
use Route69\RouteParams;
require_once '../Route69.php';
$app = new Route69();
$app->config(function (Route $route) {
    $route->when('/user/:username', ['controller' => 'user', 'resolve' => ['user' => new User(), 'wage' => rand(8.5 * 10, 15.0 * 10) / 10]]);
});
// example route: /user/fred123
$app->controller('user', function (RouteParams $routeParams, User $user, $wage) {
    $user->setUsername($routeParams->username);
    $user->setWage($wage);
    echo $user->getMessage();
});
/**
 * This is the example User class used in the controller
 * The $user parameter links to this class and is defined
 * in the resolve setting of the route config.
 */
class User
{
    protected $username = '', $wage = 8.5;
    /**
     * Sets the username
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;