Skip to content

Latest commit

 

History

198 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Total Downloads Latest Stable Version License

Installing

Requirements:

  1. Recommended: PHP 8 (see the currently supported versions at https://www.php.net/supported-versions.php)
    • Minimum: PHP 5.4 (backward compatibility is preserved for environments with upgrade limitations)
    • If you need a full-featured server on Windows or macOS, consider using WampServer, XAMPP, Laragon, EasyPHP, or AMPPS.
  2. (Optional) The Intl PHP extension, required for the Inphinit\Utility\Strings class.
  3. (Optional) The COM or cURL PHP extension, required for the Inphinit\Filesystem\Size class.

After installing PHP, you can install Inphinit via Composer or Git.

To install via Composer, run the command (see more details at https://getcomposer.org/doc/03-cli.md):

php composer.phar create-project inphinit/inphinit my-application

If Composer is installed globally, use:

composer create-project inphinit/inphinit my-application

To install via Git:

git clone --recurse-submodules https://github.com/inphinit/inphinit.git my-application
cd my-application

Testing

After navigating to your project directory, run the following command to start the PHP built-in web server:

./run serve

On Windows:

run serve

Then access it in your browser at http://localhost:5000/.

nginx

If you want to use nginx, you can configure your nginx.conf as follows:

location / {
    root /home/foo/bar/my-application;

    # Redirect page errors to route system
    error_page 403 /index.php/RESERVED.INPHINIT-403.html;
    error_page 500 /index.php/RESERVED.INPHINIT-500.html;

    try_files /public$uri /index.php?$query_string;

    location = / {
        try_files $uri /index.php?$query_string;
    }

    location ~ /\. {
        try_files /index.php$uri /index.php?$query_string;
    }

    location ~ \.php$ {
        # Replace with your FPM or FastCGI address
        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;
        include fastcgi_params;

        set $teeny_suffix "";

        if ($uri != "/index.php") {
            set $teeny_suffix "/public";
        }

        fastcgi_param SCRIPT_FILENAME $realpath_root$teeny_suffix$fastcgi_script_name;
    }
}

Note: For PHP-FPM (FastCGI Process Manager), use fastcgi_pass unix:/var/run/php/php<version>-fpm.sock (replace <version> with your PHP version).

Folder structure

├───.env.sample                # During installation, this file is automatically copied to `.env`.
├───.htaccess                  # Configures routing and common HTTP error handling for Apache server.
├───web.config                 # Configures routing and common HTTP error handling for IIS server.
├───Caddyfile                  # Configures routing and common HTTP error handling for Caddy and FrankenPHP.
├───index.php                  # Application entry point; configures autoloading and directory structures.
├───run                        # Executes CLI commands defined in `system/console.php`.
├───run.bat                    # Executes CLI commands defined in `system/console.php` on Windows.
├───public/                    # Contains static assets and public-facing PHP stand-alone scripts.
│   └───.htaccess              # Configures Apache behavior for public assets and stand-alone scripts.
└───system/                    # Contains the core application source code.
    ├───console.php            # Defines custom CLI commands.
    ├───dev.php                # Acts as `main.php` specifically for the development environment.
    ├───errors.php             # Handles rendering for custom SAPI-generated error pages.
    ├───main.php               # Defines application routing and bootstrap logic.
    ├───boot/                  # Contains autoloader settings, MIME types, and metadata.
    │   ├───importpackages.php # Imports installed Composer packages into the application autoloader.
    │   ├───media_types.php    # Maps file extensions to Content-Type headers for PHP's built-in web server.
    │   └───namespaces.php     # Indexes Composer package namespaces (generated by importpackages.php).
    ├───configs/               # Contains configuration files.
    │   └───debug.php          # Configures debugging, a shortcut to your editor, and assistant.
    ├───Controllers/           # Contains controller classes invoked by route handlers.
    ├───Commands/              # Contains command classes used by the built-in CLI interface.
    ├───storage/               # Contains application-generated files (e.g., caches, logs, temporary).
    ├───vendor/                # Contains third-party dependencies managed by Composer and core framework.
    └───views/                 # Contains view files.

In development mode, the system/dev.php script is always executed first, followed by system/main.php. If an error occurs (e.g., 404 or 405), the last script to run will be system/errors.php.

Creating routes

To create a new route, edit the system/main.php file. If you want the route to be available only in development mode, edit the system/dev.php file instead.

The routing system supports controllers, callables, and anonymous functions. For example:

<?php

// Anonymous function
$app->action('GET', '/closure', function () {
    return 'Hello "closure"!';
});

function foobar() {
    return 'Hello "function"!';
}

// Callable function
$app->action('GET', '/function', 'foobar');

// Callable class static method — the autoloader automatically includes the class file
$app->action('GET', '/class-static-method', ['MyNameSpace\Foo\Bar', 'hello']);

// Callable class method
$foo = new Sample;
$app->action('GET', '/class-method', [$foo, 'hello']);

// Don't need to include the Controllers namespace prefix — the framework automatically prepends it
$app->action('GET', '/controller', 'Boo\Bar::xyz');

/**
 * Controller from `./system/Controllers/Boo/Bar.php`:
 *
 * <?php
 * namespace Controllers\Boo;
 *
 * class Bar {
 *    public function xyz() {
 *        ...
 *    }
 * }
 */

Grouping routes

The route grouping system is simple and flexible. It is based on the full URL or path and supports the * wildcard, as well as the same patterns available for routes.

<?php

/*
 * Routes will only be added if the path starts with /blog/
 *
 * Examples:
 *
 * http://localhost:5000/blog/
 * http://localhost:5000/blog/post
 * http://localhost:5000/blog/search
 */
$app->scope('/blog/', function ($app, $params) {
    $app->action('GET', '/', function () { ... });
    $app->action('POST', '/post', function () { ... });
    $app->action('GET', '/search', function () { ... });
});

// Routes will only be added if you are accessing via HTTPS
$app->scope('https://*', function ($app, $params) {
    ...
});

// Routes will only be added if you are accessing via HTTP
$app->scope('http://*', function ($app, $params) {
    ...
});

// Routes will only be added when the request host is mysite2.org
$app->scope('*://mysite2.org/', function ($app, $params) {
    ...
});

// Routes will only be added if you are accessing a subdomain of main.org, e.g., site1.main.org
$app->scope('*://*.main.org/', function ($app, $params) {
    ...
});

// Using a pattern to capture the subdomain:
$app->scope('*://<subdomain>.main.org/', function ($app, $params) {
    $subdomain = $params['subdomain'];
    ...
});

// Using a pattern to capture path parameters:
$app->scope('*://*/users/<id:num>/<user>', function ($app, $params) {
    $id = $params['id'];
    $username = $params['user'];
    ...
});

See more examples in the system/dev.php file.

Route and URL patterns

Type Example Description
alnum $app->action('GET', '/baz/<video:alnum>', ...); Accepts only alphanumeric parameters; $params returns ['video' => ...]
alpha $app->action('GET', '/foo/bar/<name:alpha>', ...); Accepts only alphabetic parameters; $params returns ['name' => ...]
decimal $app->action('GET', '/baz/<price:decimal>', ...); Accepts only decimal number parameters; $params returns ['price' => ...]
num $app->action('GET', '/foo/<id:num>', ...); Accepts only integer parameters; $params returns ['id' => ...]
nospace $app->action('GET', '/foo/<nospace:nospace>', ...); Accepts any characters except spaces, such as %20 or tabs (see the \S regex pattern)
uuid $app->action('GET', '/bar/<barcode:uuid>', ...); Accepts UUID-formatted parameters; $params returns ['barcode' => ...]
version $app->action('GET', '/baz/<api:version>', ...); Accepts parameters in Semantic Versioning 2.0.0 (SemVer) format; $params returns ['api' => ...]

You can add or modify existing patterns using the $app->setPattern(name, regex) method. Example:

<?php
use Inphinit\Viewing\View;

$app->action('GET', '/about/<lang:locale>', function ($params) {
    $lang = $params['lang'];
    ...
});

$app->action('GET', '/product/<id:customid>', function ($params) {
    $id = $params['id'];
    ...
});

$app->setPattern('locale', '[a-z]{1,8}(\-[A-Z\d]{1,8})?'); // examples: en, en-US, en-GB, pt-BR, pt
$app->setPattern('customid', '[A-Z]\d+'); // examples: A0001, B002, J007

Modifying an existing pattern:

<?php

// Replace SemVer with <major>.<minor>.<revision>.<build>
$app->setPattern('version', '\d+\.\d+\.\d+\.\d+');

// Replace SemVer with <major>.<minor> (useful for web APIs)
$app->setPattern('version', '\d+\.\d+');

Documentation

The documentation is maintained in a separate GitHub repository.

Releases

Packages

Used by

Contributors

Languages