PHPackages                             dbarcinas/atom - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. dbarcinas/atom

ActiveLibrary[Framework](/categories/framework)

dbarcinas/atom
==============

The procedural PHP micro framework

00PHP

Since Jan 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/dbarcinas/atom)[ Packagist](https://packagist.org/packages/dbarcinas/atom)[ RSS](/packages/dbarcinas-atom/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Atom
====

[](#atom)

The procedural PHP micro framework.

Features
--------

[](#features)

- Routing
- Templates
- [Inertia](https://inertiajs.com/) (WIP)

Installation
------------

[](#installation)

Atom requires [PHP](https://php.net/) &gt;= 8.2

```
composer require dbarcinas/atom
```

Configuration
-------------

[](#configuration)

```
use function atom\config;

// Set config
config('app.name', 'Atom');
config('foo', 'bar');

// Get config
$name = config('app.name');
echo $name; // 'Atom'

// Retrieve all config
$config = config();
print_r($config);

// Prints out
[
    'app' => [
        'name' => 'Atom',
    ],
    'foo' => 'bar'
]

// Configuration can be retrieved using dot notation
echo config('foo'); // 'bar'
echo config('app.name') // 'Atom'
```

Routing
-------

[](#routing)

**Basic Usage**

```
use function atom\{dispatch, route, url_for};

// Basic route
route('GET', '/foo', function () {
    // ...
});

// Multiple methods
route(['GET', 'POST'], '/bar', function () {
    // ...
});

// Route names
// Method verbs can be lowercased
route(['put'], '/baz', function () {
    // ...
}, '@baz');

route('get', '/qux', function () {
    echo url_for('@baz'); // prints '/baz'
});

// Dispatch
dispatch();
```

**Route Parameters**

```
route('GET', '/foo/{id}', function ($id) {
    echo "ID: $id";
});

// Parameter types
route('POST', '/foo/bar/{id:int}, function ($id) {
    echo "POST ID: $id";
});
```

**Middlewares**

```
// Auth middleware
function auth(string $request, string $method, array $params): bool {
    if ($params['id'] > 1) {
        return false;
    }
    // Must return boolean
    return true;
}

route('get', '/foo/{id:int}', function ($id) {
    // ...
}, '@foo', 'auth');

// Multiple middlewares
route('get', '/bar', function () {
    // ...
}, '@bar', ['auth', 'logger']);
```

**Route Groups**

```
use function atom\{dispatch, stack};

// Grouping routes in atom are called stacks
// $route is delegated to 'route(...)'
stack('/api/user', function ($route) {
    $route('GET', '/{id:int}' function ($id) {
        // Maps to '/api/user/1'
        // ...
    });

    // Middleware example
    $route(['GET', 'POST'], '/feed', function () {
        // Maps to '/api/user/feed'
        // ...
    }, '@api.user.feed', ['auth']);
});

// Middleware
stack('/dashboard', function ($route) {
    // ...
}, ['auth', 'logger']);

dispatch();
```

**Custom 404 Page**

```
use function atom\{route, dispatch};

// Routes
// route(...);
// route(...);

// Dispatch with a custom error handler
dispatch(function ($request, $method) {
    echo '404 Not Found.';
});
```

**Note:** Routes are automatically cached for faster response and improved performance speed.

Templates
---------

[](#templates)

**Basic Usage**

```
use function atom\{config, dispatch, route, render}

// Set templates path
config('templates.path', __DIR__ . '/../templates');

route('get', '/foo', function () {
    // '.php' extension is optional
    render('foo', [
        'bar' => 'baz',
    ]);
});

dispatch();

// /templates/foo.php

  foo

```

**Layouts**

```
// /templates/layout.php
