DEV Community

Cover image for Next-gen PHP Framework Lynx
Signor_P
Signor_P

Posted on

Next-gen PHP Framework Lynx

NestJS + PHP = Lynx

Greetings, how are you? I hope your code and errors are plentiful. I'm back here after a long time. Let me briefly introduce myself. I'm a self-employed developer/entrepreneur who has been designing backend and system architecture with TS for 3-4 years. Everyone online knows me as Signor P. I'm here to announce that I've released my Lynx framework as open source, as I mentioned to you in the past. Lynx started as a hobby project, and then I asked myself, why not share it with other developers and develop it together? πŸ˜…. Briefly, Lynx is a backend framework written in pure PHP with a NestJS architecture and syntax. It's a framework with a small CLI tool written in Rust and a database system with a small TypeORM syntax. Developers who want more technical details can check the Github repositories. So why did I write Lynx... I sometimes need to write PHP or want to write PHP. But huge frameworks like Laravel tire me out. Minimal and spacious frameworks are rare in the PHP world. As a TS developer myself, I thought, why not port a TS framework to PHP? And I decided that NestJS was the most suitable framework for this. I use Lynx myself in some hobby projects and prototypes. When I needed to write a manager for database operations, I thought, why not use the TypeORM syntax that I always use and that is compatible with NestJS? And although not exactly the same, I added a database layer in a similar TypeORM style. Entities, Repository usage, etc... Currently, Lynx works for small projects and some medium-sized projects. However, it's worth mentioning that I am not a security expert or professional software engineer. Yes, I have a few successful projects that I haven't published yet, but I should point out that I'm not that good at PHP. Let's take a brief look at the project architecture.

πŸ“ your-project-root
β”œβ”€β”€ πŸ“ App                         # 🐾 LYNX CORE ENGINE (Zero dependencies, pure PHP)
β”‚   β”œβ”€β”€ πŸ“ Builder                 # Fluent factories (e.g. ExceptionBuilder)
β”‚   β”œβ”€β”€ πŸ“ Cache                   # Temporary runtime caching & logger.json storage
β”‚   β”œβ”€β”€ πŸ“ Config                  # Framework system configuration
β”‚   β”œβ”€β”€ πŸ“ Core                    # Kernel, Request, Response, Router, Autoloader, & Repositories
β”‚   β”œβ”€β”€ πŸ“ Decorators              # Core attributes (Module, Controller, Column, GET/POST, Inject, etc.)
β”‚   β”œβ”€β”€ πŸ“ Exception               # Built-in HTTP exceptions (BadRequest, NotFound, etc.)
β”‚   β”œβ”€β”€ πŸ“ Helper                  # Console logs, secure encryption, tokenizers, & .env loaders
β”‚   β”œβ”€β”€ πŸ“ Interface               # LogLevels, Middleware, & ExceptionFilter contracts
β”‚   └── πŸ“ Templates               # Default engine templates (e.g. default 404 page)
β”œβ”€β”€ πŸ“ Src                         # πŸ—οΈ APPLICATION DOMAIN CODE (Write your code here)
β”‚   β”œβ”€β”€ πŸ“ views                   # HTML template 'welcome' views & custom 404s
β”‚   β”œβ”€β”€ πŸ“„ AppController.php       # Root application routes controller
β”‚   β”œβ”€β”€ πŸ“„ AppMiddleware.php       # Root application middleware pipeline
β”‚   └── πŸ“„ AppModule.php            # Root AppModule configuration (orchestrates sub-modules)
β”œβ”€β”€ πŸ“„ .env                        # Environment Configuration (DB credentials, status, etc.)
β”œβ”€β”€ πŸ“„ .htaccess                   # Apache URL-rewriting configuration
└── πŸ“„ index.php                   # Core application entrypoint (boots LynxApplication)
Enter fullscreen mode Exit fullscreen mode
<?php

// index.php

require_once './App/Core/AutoLoader.php';

use App\Core\LynxApplication;
use Src\AppModule;

global $app;
$app = new LynxApplication(AppModule::class);
$app->run();
Enter fullscreen mode Exit fullscreen mode

// Src/AppModule.php

<?php

namespace Src;

use App\Decorators\Module;
use Src\AppController;

#[Module([
    'controllers' => [AppController::class]
])]
class AppModule {}
Enter fullscreen mode Exit fullscreen mode
<?php

// Src/AppController.php

namespace Src;

use App\Core\Request;
use App\Core\Response;
use App\Decorators\Controller;
use App\Decorators\Get;
use App\Decorators\Post;
use App\Decorators\Middleware;

#[Controller()]
#[Middleware([AppMiddleware::class])]
class AppController
{
    #[Get('/')]
    public function get(Request $req, Response $res): void
    {
        $res->render('welcome', ['name' => 'Lynx']);
    }

    #[Post('/')]
    public function logout(Request $req, Response $res): void
    {
        $res->json([
            "framework" => "Lynx"
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode
<?php

// Src/AppMiddleware.php

namespace Src;

use App\Core\Request;
use App\Core\Response;
use App\Helper\Console;
use App\Interface\Middleware;

class AppMiddleware implements Middleware
{
    public function use(Request $req, Response $res, mixed $next): void
    {
        Console::log('info', 'Run App Middleware');
        $res->setHeader('X-Powered-By', 'Zexson Team');
        $next($req, $res);
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see, you can develop PHP backend applications in the style of NestJS. I enjoy using Lynx and I like it very much. I hope this open-source project hasn't wasted your time. That's all for now. The necessary links are below. I eagerly await your feedback 🐾

πŸ¦„

Top comments (0)