PHP code example of devsrealm / tonics-router-system
1. Go to this page and download the library: Download devsrealm/tonics-router-system 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/ */
devsrealm / tonics-router-system example snippets
use Devsrealm\TonicsRouterSystem\Container\Container;
use Devsrealm\TonicsRouterSystem\Events\OnRequestProcess;
use Devsrealm\TonicsRouterSystem\Handler\Router;
use Devsrealm\TonicsRouterSystem\RequestInput;
use Devsrealm\TonicsRouterSystem\Resolver\RouteResolver;
use Devsrealm\TonicsRouterSystem\Response;
use Devsrealm\TonicsRouterSystem\Route;
use Devsrealm\TonicsRouterSystem\RouteNode;
use Devsrealm\TonicsRouterSystem\RouteTreeGenerator;
use Devsrealm\TonicsRouterSystem\State\RouteTreeGeneratorState;
$onRequestProcess = new OnRequestProcess(
new RouteResolver(
new Container()
),
new Route(
new RouteTreeGenerator(
new RouteTreeGeneratorState(), new RouteNode()
)
)
);
$router = new Router(
$onRequestProcess,
$onRequestProcess->getRouteObject(),
new Response(
$onRequestProcess, new RequestInput()
)
);
$route = $router->getRoute();
$route->get('/', function() {
return 'Welcome To My Home Page';
});
// Once your route is set up, dispatch it (don't forget to do this once all your route is set-up, otherwise, it won't work):
try {
$router->dispatchRequestURL();
} catch (Exception $e) {
// handle error or 404
}
class Authenticated implements TonicsRouterRequestInterceptorInterface
{
/**
* @inheritDoc
*/
public function handle(OnRequestProcess $request): void
{
if (UserData::isAuthenticated() === false){
# If this is for admin, then redirect to admin login
if (str_starts_with($request->getRequestURL(), '/admin')){
redirect(route('admin.login'));
}
# If this is for customer, then redirect to customer login
if (str_starts_with($request->getRequestURL(), '/customer')){
redirect(route('customer.login'));
}
# Else...
SimpleState::displayUnauthorizedErrorMessage();
}
}
}