Inphinit v2.1
Inphinit v2.1 is a PHP micro-framework designed for developers who value simplicity, flexibility, and control when building web applications. Despite its minimal footprint, it provides a useful set of features for workin
Inphinit v2.1 is a PHP micro-framework designed for developers who value simplicity, flexibility, and control when building web applications. Despite its minimal footprint, it provides a useful set of features for working with routing, filters, command-line tools, task scheduling, data validation, HTML document processing, and Markdown conversion.
One of its key advantages is that it can be used without Composer, directly through PHP files. The dependency manager remains optional for developers who want to add external libraries. Inphinit is compatible with PHP 8 while also maintaining backward compatibility with PHP 5.4.
Compatible with Multiple Web Servers
Inphinit can run across a variety of hosting environments and web servers without requiring a specific infrastructure.
Supported web servers include:
- Apache
- FrankenPHP
- lighttpd
- Nginx
- IIS
This flexibility makes it suitable for everything from shared hosting environments to custom server infrastructures.
Routing System
Routing is one of Inphinit's core features. Routes are defined in the system/main.php file, where URL paths and HTTP methods can be mapped to functions, closures, class methods, or controllers.
The routing system supports several HTTP methods, including GET, POST, PUT, PATCH, and DELETE. It also supports dynamic parameters with predefined patterns, allowing developers to restrict accepted values directly in route definitions.
For example, the following route accepts a numeric parameter:
$app->action('GET', '/users/<id:num>', function ($app, $params) {
return 'User: ' . $params['id'];
});
In this example, the id parameter only accepts integer values.
Grouping Routes with Scopes
For applications with a large number of routes, Inphinit provides a scope system that makes it easier to group related paths and keep the application organized.
Using $app->scope(), developers can define a common prefix for multiple routes. Scopes can also be restricted by domain, subdomain, or HTTP/HTTPS protocol.
Here's an example:
$app->scope('/admin/', function ($app, $params) {
$app->action('GET', '/', function () {
return 'Admin dashboard';
});
$app->action('GET', '/users', function () {
return 'User management';
});
});
In this case, both routes are organized under the /admin/ prefix, resulting in the following paths: /admin/ and /admin/users.
Route Filters
Another important feature is the filter system, which follows a concept similar to middleware in other frameworks.
Filters allow developers to intercept incoming requests before they reach their corresponding routes. Depending on specific conditions, a filter can either allow a request to proceed or block access.
Filters can be registered globally or within a specific scope, making it easier to implement authentication, authorization, and other access control rules.
For example, an authentication filter could look like this:
use Inphinit\Http\Request;
$app->useFilter(function ($app, $method, $path, $params) {
if (!isAuthenticated()) {
Request::status(401);
return false;
}
return true;
});
When the authentication condition is not met, the filter prevents access to protected routes. The isAuthenticated() function represents an authentication check implemented by the application itself.
Command-Line Tools and Task Scheduling
Inphinit also provides features for running operations outside the traditional HTTP request-response cycle.
Creating Custom Commands
With its console command system, developers can create custom commands and execute them directly from the terminal.
Commands can be defined using closures, functions, or class methods, and can also accept arguments and options.
Command definitions are registered in the system/console.php file.
Here's an example of a custom command:
use Inphinit\Experimental\Cli\Command;
$console->action('hello', function (
Command $command,
array $options,
array $residues
) {
echo 'Hello, terminal!';
});
Once registered, the command can be executed from the project's root directory using ./run hello.
This makes it possible to build maintenance tools, administrative routines, and application-specific operations without having to create web interfaces for those tasks.
Scheduling Tasks
In addition to console commands, Inphinit includes a task scheduling system for running operations repeatedly or just once.
You can schedule functions, existing commands, and even operating system commands. Schedules can be configured using intervals, specific times, or Cron-like expressions.
For example, the following task runs once every hour:
use Inphinit\Experimental\Scheduling\Task;
$task = $scheduler->call('hourly-report', function (Task $task) {
generateReport();
});
$task->interval(3600);
Task schedules are defined in the system/console.php file. To run them automatically, you need to configure the operating system's scheduling service, such as Cron, Windows Task Scheduler, or launchd.
This allows Inphinit to handle automated processes without relying on users to access the application.
Field Validation
Handling form submissions requires care. Invalid or unexpected input can interfere with an application's behavior, so submitted data should be validated before it is processed.
To make this easier, Inphinit provides the Validator class, which allows developers to define validation rules directly for incoming fields.
Available rules include:
-
required: makes a field mandatory; -
minandmax: set minimum and maximum character counts or numeric values; -
email: validates email addresses; -
url: validates URLs; -
integeranddecimal: validate numeric values; -
boolean: validates boolean values; -
regex: allows custom regular expressions; -
in: restricts accepted values to a predefined list; -
equal: compares the contents of two fields; -
cnpj: validates a Brazilian CNPJ number; -
cpf: validates a Brazilian CPF number; -
password: checks whether a password meets strength requirements.
POST request data can be validated as follows:
use Inphinit\Experimental\Validation\Validator;
$validator = Validator::post();
$validator->field('name', $name, 'required|min:3|max:80');
$validator->field('email', $email, 'required|email');
if ($validator->valid()) {
// Process the validated data
} else {
$errors = $validator->failures();
}
The valid() method indicates whether all fields have passed their respective validation rules. When errors are found, failures() provides the corresponding error messages.
This approach reduces the amount of code needed to validate forms and makes it easier to organize incoming data handling.
Reverse Proxy and Load Balancer Support
In modern hosting environments, incoming requests often pass through one or more intermediary services before reaching the server running PHP.
This is common with services such as Cloudflare, AWS Application Load Balancer (ALB), AWS Elastic Load Balancer (ELB), and CloudFront.
These services can act as reverse proxies, load balancers, or connection intermediaries. Besides distributing incoming requests, they may also handle TLS termination, accepting HTTPS connections from users and forwarding requests to the application server over HTTP.
In these scenarios, PHP may identify the proxy's IP address as the source of the connection instead of the visitor's actual IP address. It may also incorrectly identify the original protocol used by the client.
To address this, proxies can forward request information through HTTP headers, such as:
-
X-Forwarded-For: the client's original IP address; -
X-Forwarded-Proto: the original request protocol; -
X-Forwarded-Host: the original host; -
X-Forwarded-Prefix: the forwarded path prefix.
Inphinit includes dedicated support for these scenarios, allowing the framework to interpret this information and make it available to the application.
To enable this feature, set the APP_FORWARDED=1 environment variable on the server or in the .env file.
Next, in system/main.php, register the IP addresses or networks of trusted proxies using $forwarded->setKnownProxies() or $forwarded->setKnownNetworks().
For Cloudflare, for example, you should use the official IP ranges published by the service and keep them up to date. The same approach applies to other providers, using the addresses or networks associated with each infrastructure.
Once configured, the framework makes forwarded connection information available through $app->forwarded, including methods such as getIp(), getProto(), getHost(), and getPrefix().
Configuring trusted proxies is essential for security. Forwarded headers should never be trusted indiscriminately, since any client may attempt to forge this information. Always register only authorized intermediaries and, whenever possible, restrict direct access to the application server.
HTML Parsing and CSS Selectors
Processing HTML documents is a common requirement in many types of applications.
Whether you're building a web scraper, extracting information from external pages, reading HTML documents, or manipulating structured content, having an HTML parser can make the job considerably easier.
Inphinit provides tools for working with HTML and XML documents through the Inphinit\Dom\Document class, along with support for CSS selectors to locate elements.
One of the main advantages of this approach is how easy the selectors are to read.
Developers who already work with web interfaces are familiar with CSS expressions such as #content, .title, article p, and ul > li. That same familiarity can be put to use in PHP when querying documents.
XPath, by comparison, uses its own expressions to navigate document structures. While it is powerful and offers advanced querying capabilities, its syntax can feel less intuitive for everyday selections.
With CSS selectors, common operations become easier to write, understand, and maintain.
Inphinit provides methods such as first(), all(), and count(), allowing developers to retrieve the first matching element, get all matching elements, or count the matches.
Selectors can also be used with native PHP DOMDocument instances, providing additional integration possibilities for existing code.
It's worth noting that the parser works with the document it receives or loads. Retrieving an external page, when required for web scraping, is a separate step from processing the HTML itself.
Working with Markdown Documents
Markdown is a lightweight markup format that lets you write content using a simple, readable syntax.
Its main advantage is that documents can be created without having to write HTML directly.
Headings, lists, links, images, blockquotes, and code snippets can all be expressed with straightforward syntax, making content easier to create and maintain.
This format is particularly useful for blogs, documentation systems, content pages, and applications that need to store text in an organized way.
Inphinit provides a conversion class that turns Markdown text or files into HTML.
To convert a file, simply use the fromFile() method:
use Inphinit\Experimental\Utility\Markdown;
$markdown = new Markdown();
$html = $markdown->fromFile(
INPHINIT_SYSTEM . '/storage/article.md'
);
echo $html;
You can also convert a string directly using fromString().
The result is HTML content that can be incorporated into application pages. By default, the parser does not allow arbitrary HTML inside Markdown content. This behavior can be explicitly changed by calling enableHtml().
This makes it possible to keep content in a simple, readable format while letting the framework handle the conversion to HTML.
A Flexible Foundation for Different Projects
Inphinit v2.1 brings together a set of features designed to address different web development needs without compromising its minimalist philosophy.
Its routing system provides flexibility when organizing applications, filters help manage access control, command-line tools and task scheduling simplify automation, and document processing utilities expand the possibilities of working with PHP.
Another advantage is that the framework can be gradually introduced into existing projects without requiring a complete application migration.
For developers looking for a micro-framework that offers implementation freedom, compatibility with different environments, and practical features without imposing an overly complex structure, Inphinit is worth exploring.
Discover Inphinit v2.1 and see what you can build with it:
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.