Ejemplo n.º 1
0
<?php

use app\classes\Language;
use app\classes\User;
use app\controllers\AdminController;
use app\controllers\ExampleController;
use js\tools\commons\http\Request;
use simpleframe\routing\Route;
use simpleframe\routing\Router;
// UrlParameter names in route URL must match handler parameter names.
// This is to be sure that we correctly check the types and cast the values of the parameters.
Router::addRoute('index', '/{lang}/{user}', function (Language $lang, User $user = null) {
    return 'Hello, ' . ($user ? $user->getName() : 'World') . '! Your locale is ' . $lang->getValue();
});
Router::addRoute('index2', '/{lang}/foo-{user}-bar', function (Language $lang, User $user = null) {
    // it is awkward if you don't specify the parameter (/foo--bar), but it works
    return 'Hello, ' . ($user ? $user->getName() : 'World') . '! Your locale is ' . $lang->getValue();
});
Router::addRoutes((new Route('index3', '/foo/{bar}', function (string $bar, Request $request) {
    return $bar . '|' . $request->getUri();
}))->setRequireHttps(true));
// route names have no strict scheme; anything (reasonable) will work
Router::addRoute('example.index', '/example', [ExampleController::class, 'index']);
Router::addRoute('example.put', '/example/put', [ExampleController::class, 'put'], ['put']);
Router::addRoute('example.test', '/example/test', [ExampleController::class, 'test']);
Router::addRoute('example.test2', '/example/test2/{user}/{id2}', [ExampleController::class, 'test2']);
Router::addRoute('admin.index', '/admin', [AdminController::class, 'index']);
Router::addRoute('admin.login', '/admin/login', [AdminController::class, 'login']);
Router::addRoute('admin.logout', '/admin/logout', [AdminController::class, 'logout']);
Ejemplo n.º 2
0
 public static function index(Request $request, TestInjection $ti)
 {
     Logger::log(Logger::INFO, 'example log message');
     return 'path: ' . $request->getUri()->getPath() . ', random injection data: ' . $ti->getSomeData() . ', next up: ' . Router::link('example.test2', ['user' => new User(1)])->setQueryParameters(['foo' => 'b a r'])->getAbsolute();
 }
Ejemplo n.º 3
0
 private static function render()
 {
     if (isset($GLOBALS['argc']) && $GLOBALS['argc'] > 0) {
         $argv = $GLOBALS['argv'] ?? [];
         if (!isset($argv[1], $argv[2]) || !in_array($argv[1], Request::METHODS)) {
             if (isset(self::$consoleHandlers[$argv[1]])) {
                 self::$consoleHandlers[$argv[1]](...array_slice($argv, 2));
                 exit;
             }
             echo 'Examples:', PHP_EOL, "\tphp index.php method path[ data]", PHP_EOL, "\tphp index.php get /foo", PHP_EOL, "\tphp index.php get -s /foo/bar a=1&b=2", PHP_EOL, "\tphp index.php post /foo/bar a=1&b=2", PHP_EOL, "\tphp index.php delete /foo/bar a=1&b=2", PHP_EOL, "\t OR", PHP_EOL, "\tphp index.php command[ arguments]";
             exit(1);
         }
     }
     Router::render(self::getRequest());
 }
Ejemplo n.º 4
0
<?php

// Put any custom startup actions here, like initializing database connections, registering shutdown handlers, etc.
// This ensures maximum flexibility as you can use whatever tools you want and are not bound by the framework.
use app\classes\ExampleConsoleHandler;
use app\classes\Session;
use js\tools\commons\http\Request;
use simpleframe\App;
use simpleframe\EventHandler;
use simpleframe\responses\RedirectResponse;
use simpleframe\routing\Route;
use simpleframe\routing\Router;
EventHandler::on(EventHandler::ON_ROUTE_MATCH, function (Route $route, array $parameters, Request $request) {
    if ($route->getRequireHttps() && !$request->isSecure()) {
        $url = Router::link($route->getName(), $parameters)->setScheme('https');
        (new RedirectResponse($url))->render();
    }
    if (substr($route->getName(), 0, 5) === 'admin') {
        if (!Session::isLoggedIn() && $route->getName() !== 'admin.login') {
            (new RedirectResponse(Router::link('admin.login')))->render();
        }
    }
});
App::registerConsoleHandler(ExampleConsoleHandler::class);
session_start();
Ejemplo n.º 5
0
 public static function logout()
 {
     Session::logout();
     return new RedirectResponse(Router::link('admin.login'));
 }