SimpleRouter image

Module Description

With URL routing, you can configure your application to handle request URLs that don't directly correspond to physical files. This router enables you to map URL patterns to specific handlers, which can be implemented as closures or controller methods responsible for processing the requests.

The SimpleRouter is an excellent choice for building REST APIs or utilizing your Scriptor application as a headless CMS.

Installation Instructions

To install the SimpleRouter module, you have two options:

  1. Clone the module into your site/modules/ directory.
  2. Download the zip file, extract its contents, and upload the complete SimpleRouter folder to your server's site/modules/ directory.

To access the Module Manager, click on the "Modules" button, which will take you to the Module Manager section.

Next, locate the "SimpleRouter" module in the list and click on the "Install" button. This action will initiate the installation process, and once completed, the module will be successfully installed in your Scriptor application.

Next, create the _routes.php file in the site/themes/[your-theme-folder] directory.

That's it! You can now start using your router. All Scriptor routes are defined in the _routes.php file located in the site/themes/[your-theme-folder] directory. This file is automatically loaded into your application by _ext.php, so you don't need to worry about including it manually.

Usage

Let's explore a few more examples in the _routes.php file to demonstrate how easily you can create custom routes for your application.

To add new routes to your application, you can utilize the Route::add() method. This method accepts up to three parameters: 1. the route (path), 2. the closure or controller method to be executed when the route is matched, 3. the HTTP verb(s) associated with the route.

_routes.php:

<?php
use Scriptor\Modules\SimpleRouter\Route;

Route::add('/', function() {
    echo 'Welcome to SimpleRouter for Scriptor CMS!';
}, 'GET');

// Execute router
Route::run();

When you visit your website, for example, using https://your-site.com/, you should see the output "Welcome to SimpleRouter for Scriptor CMS!" displayed.

More examples

At times, you may need to register a route that responds to multiple HTTP verbs. Here's an example of a route that matches both GET and POST methods:

Route::add('/info', function() {
    echo 'This is a simple URL Router module for Scriptor CMS.';
}, ['GET', 'POST']);

In certain scenarios, you may need to capture a parameter (e.g., the ID of a user) from the URL. This can be achieved by defining route parameters:

Route::add('/users/{id}', function($id) {
    echo 'This is the user ID: '. (int) $id;
}, 'GET');

You can define as many route parameters as needed:

Route::add('/users/{user}/posts/{post}', function($user_id, $post_id) {
    echo 'This is the user ID: '. (int) $user_id .' & post ID: '.(int) $post_id;
}, 'GET');

Instead of using a closure, you can call a controller method (note: you must create a controller module first. You can learn how to create a module for Scriptor by reading the following guide):

Route::add('/users/{user}/posts/{post}', 
    'Scriptor\Controllers\YourModule::yourMethod', 
    'GET'
);

Route Groups

Route groups allow you to combine various routes of a specific method together:

// Group all routes of the POST method:
Route::processor()->matchMethod('POST', function() {

    Route::add('/contact-form', function() {
        echo 'Post contact form data...';
    }, 'POST');

    Route::add('/subscribe-form', function() {
        echo 'Post subscription form data...';
    }, 'POST');

    // ...
});

Special methods

You can add a method that will be executed if none of the specified routes match. Typically, a "404 Not Found" message is displayed:

// Add a function for handling 404 not found
Route::routeNotMatch(function($path) {
    header('HTTP/1.0 404 Not Found');
    echo '<h4>Error 404</h4>';
    echo '<p>The requested path "'.$path.'" was not found!</p>';
});

In some cases, the specified route may match, but the HTTP verb does not. In such situations, you can define a method to send a "405 Method Not Allowed" response:

// Add a 405 method
Route::methodNotAllowed(function($uri, $method) {
    header('HTTP/1.0 405 Method Not Allowed');
    echo '<h4>Error 405</h4>';
    echo '<p>The requested URI "'.$uri.'" exists, but the request method "'.
          $method.'" is not allowed!</p>';
});

Requirements

Compatible with Scriptor: 1.11.3 +
PHP 8.1 +
Author: Bigin
GitHub repo
Last Updated: 2023-06-21
SimpleRouter: 1.5.1