PHP code example of folded / routing

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

    

folded / routing example snippets


use function Folded\addGetRoute;
use function Folded\matchRequestedUrl;

addGetRoute("/", function() {
  echo "Hello world";
});

try {
    matchRequestedUrl();
} catch (Exception $exception) {
  // ...
}

use function Folded\addGetRoute;

addGetRoute("/about-us", function() {
  echo "<h1>About us</h1>";
});

use function Folded\addPostRoute;

addPostRoute("/search/{search}", function($search) {
  // Pulling posts from the database...
  echo "<h1>Search result for $search</h1>";
});

use function Folded\matchRequestedUrl;
use Folded\Exceptions\UrlNotFoundException;

try {
  matchRequestedUrl();
} catch (Exception $exception) {
  if ($exception instanceof UrlNotFoundException) {
    // Log it, or send it to an error management system...
    // Display a 404 page...
  }
}

use function Folded\matchRequestedUrl;
use Folded\Exceptions\MethodNotAllowedException;

try {
  matchRequestedUrl();
} catch (Exception $exception) {
  if ($exception instanceof MethodNotAllowedException) {
    // Log it, or send it to an error management system...
    // Display a 405 page...
  }
}

use function Folded\addGetRoute;

addGetRoute("/", function() {
  echo "welcome home";
}, "home.index");

use function Folded\addGetRoute;
use function Folded\getRouteUrl;

addGetRoute("/user/{user}/post/{post}", function($user, $post) {
  echo "User $user posted $post";
}, "user.post.show");

echo getRouteUrl("user.post.show", ["user" => 42, "post" => 1]); // string(15) "/user/42/post/1"

use function Folded\addGetRoute;
use function Folded\redirectToRoute;

addGetRoute("/about-us", function() {
  echo "<h1>About us</h1>";
}, "about-us.index");

redirectToRoute("about-us.index");

use function Folded\addGetRoute;
use function Folded\redirectToRoute;

addGetRoute("/about-us", function() {
  echo "<h1>About us</h1>";
}, "about-us.index");

redirectToRoute("about-us.index", 200);

use function Folded\redirectToUrl;

redirectToUrl("/about-us");

use function Folded\redirectToUrl;

redirectToUrl("/", 200);

use function Folded\currentUrlIs;

if (currentUrlIs("/")) {
  echo "we are at the home page";
} else {
  echo "we are not at the home page";
}

use function Folded\currentRouteIs;
use function Folded\addGetRoute;

addGetRoute("/about-us", function() {
  echo "<h1>About us</h1>";
}, "about-us.index");

if (currentRouteIs("about-us.index")) {
  echo "this is the about us page";
} else {
  echo "this is not the about us page";
}

use function Folded\currentRouteIs;
use function Folded\addGetRoute;

addGetRoute("/user/{user}", function() {
  echo "<h1>User detail</h1>";
}, "user.show");

if (currentRouteIs("user.show", ["user" => 17])) {
  echo "this is the route /user/17";
} else {
  echo "this is not the route /user/17";
}