PHP code example of trulyao / php-router

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

    

trulyao / php-router example snippets




use \Trulyao\PhpRouter\Router as Router;

$router = new Router(__DIR__."/views", "demo");

$router->get("/", function($req, $res) {
    return $res->send("<h1>Hello World</h1>")
               ->status(200);
});

$router->get('/render', function ($req, $res) {
    return $res->render("second.html", $req);
});

$router->post("/", function($req, $res) {
   return $res->send([
       "message" => "Hello World"
   ]);
});

# using a class based controller
$router->delete("/", [new NoteController(), "destroy"]); 

$router->route("/chained")
    ->get(function ($req, $res) {
    return $res->send("GET - Chained!");
    })
    ->post(function ($req, $res) {
    return $res->send("POST - Chained!");
    });

# Start the router - very important!
$router->serve();

bash
composer 
bash
composer create-project trulyao/php-starter hello-world

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php
RewriteRule .* - [E=HTTP_CONTENT_TYPE:%{HTTP:Content-Type},L]
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
html
@php
session_start();
@endphp