fhooe/router-skeleton

An example application or skeleton for getting started with fhooe/router.

Maintainers

Package info

github.com/Digital-Media/fhooe-router-skeleton

Language:Latte

Type:project

pkg:composer/fhooe/router-skeleton

Statistics

Installs: 267

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v3.0.2 2026-05-25 09:55 UTC

This package is auto-updated.

Last update: 2026-05-25 09:56:59 UTC


README

An example application, a.k.a. skeleton for getting started with fhooe/router: the simple object-oriented router developed for PHP classes in the Media Technology and Design program at the University of Applied Sciences Upper Austria. This skeleton and the library behind it are primarily designed for educational purposes (learning the concept of routing and object-oriented principles). Use it for "public" applications at your own risk.

Creating a fhooe/router Application

Use Composer to create a new project containing the skeleton files:

composer create-project fhooe/fhooe-router-skeleton path/to/install

Composer will create a project in the specified path/to/install directory.

Basic Usage

The router invocation happens in public/index.php. This front controller file receives all the requests since the associated .htaccess file will redirect everything to it.

fhooe/router can be used in two ways:

Using a Router Object (recommended)

  1. Instantiate the Router class.

    $router = new Router();

    Adding a logger: Pass an instance of a PSR-3 compatible logger application, e.g., Monolog, to receive predefined log messages about routes being added and executed. This can be useful to track what the Router is doing.

    $logger = new Logger("skeleton-logger");
    // add processors, formatters or handlers to the logger
    $router = new Router($logger);
  2. Define routes using the get() and post() methods. Supply a URI pattern to match against and a callback that is executed when the pattern and HTTP method match.

    $router->get("/", function () {
        // e.g., load a view
    });

    Placeholders: You can define route placeholders using curly brackets. The name of the placeholder will be available as a parameter in the callback, and the actual value in the URI will be its argument.

    $router->get("/product/{id}", function ($id) {
       // e.g., load a view to display the product
    });

    Optional parts: You can make route parts optional by putting them in square brackets. That way, a route will match both ways. This can be, for example, used to make a route work with or without a trailing slash.

    $router->get("/form[/]", function () {
       // e.g., load a view
    });
  3. Set a 404 callback to load a view or trigger behavior when no route matches.

    $router->set404Callback(function () {
        // e.g., load a 404 view
    });
  4. Optional: Define a base path if your application is not located in your server's document root.

    $router->basePath = "/path/to/your/files";
  5. Run the router. This will fetch the current URI, match it against the defined routes, and execute them if a match is found.

    $router->run();

Displaying Output

Simple example view files in the form of HTML and PHP files are located in the views directory together with Latte examples for cleaner output.

Two Latte extensions have been added.

  • RouterExtension provides the functions url_for() and get_base_path() in templates for generating URLs and retrieving the base path from the Router object.
  • SessionExtension provides the function session(key) for retrieving entries in the $_SESSION superglobal.

Browsing the Application

To take a quick look, you can use the PHP built-in web server:

cd path/to/install
composer start

Navigate to http://localhost:8888/ in your browser to see the application in action.

Contributing

If you'd like to contribute, please refer to CONTRIBUTING for details.

License

fhooe/router-skeleton is licensed under the MIT license. See LICENSE for more information.