PHP code example of basecode / route

1. Go to this page and download the library: Download basecode/route 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/ */

    

basecode / route example snippets


define("DS", DIRECTORY_SEPARATOR);
define("DOMAIN", "https://localhost/project");

ntroller_method [string] [optional] [default] = ":"

    $route = new Route(domain_url, separator_controller_method);

    # EXAMPLE
    $route = new Route(DOMAIN, ":");

*/

$route = new Route(DOMAIN);

/*
    route [string] [|function]
    $route->standard(action);


    # EXAMPLE
    $route->get("admin/login", function() {
        echo "<h1>ADMIN LOGIN</h1>";
    }, "admin.login");

*/

$route->get("/", function() {
    echo "<h1>HOME PAGE</h1>";
}, "home");

$route->namespace("Controllers\\Admin")->group("admin");

$route->get("/", "Controller:login", "admin.login");

// get route to namespace: Controllers\Admin\Controller - method: login

$route->execute();
/*
    or
    $route->execute("route"); // route is the name received by $_GET
*/

$route = new Route(DOMAIN);

$route->get("/", function() {
    echo "<h1>HOME PAGE</h1>";
}, "home");

$route->standard(function($error) {
    http_response_code($error["code"]);
    echo "<h1>ERROR: ".$error["message"]."</h1>";
});

$route->execute();

$route = new Route(DOMAIN);

$route->get("/", function() use ($route) {

    $link = $route->route("landing.page");
    echo "<h1>HOME PAGE</h1><br><a href=\"{$link}\">Landing</a>";

}, "home");

$route->get("landing", function() use ($route) {

        $link = $route->route("home");
        echo "<h1>LANDING PAGE</h1><br><a href=\"{$link}\">Back Home</a>";

}, "landing.page");

$route->execute();

$route = new Route(DOMAIN);

$route->get("hello/{name}", function($data) {

    $name = $data["name"];
    echo "<h1>HELLO {$name}</h1>";

}, "hello.page");

$route->execute();

$route = new Route(DOMAIN);

$route->get("product/{id}", function() use ($route) {

    $current = $route->current();
    echo "<p>ROUTE: {$current}</p>";

}, "product.page");

$route->execute();

$route = new Route(DOMAIN);

// REDIRECT OPTIONS

/* USING NAME

    ROUTES EXAMPLES (
        $route->get("/home", route_action, "page.home");
        $route->get("/product/{id}", route_action, "page.product");
    )

    $route->redirect("page.home"); // redirect to domain/home
    $route->redirect("page.product", ["id" => 10]); // redirect to domain/product/10
*/

/* USING URL
    $route->redirect("https://www.google.com"); // redirect to passed URL
*/

$route->group("admin");

$route->get("/login", function() {

    echo "<h1>ADMIN LOGIN</h1>";

}, "admin.login");

$route->get("/home", function() use ($route) {

    if (!isset($_SESSION["ADMIN_USER"])) {
        $route->redirect("admin.login");
    }

    echo "<h1>ADMIN HOME</h1>";

}, "admin.home");

$route->execute();
apacheconf
RewriteEngine On
Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1