nextphp

NextPHP | Next Generation PHP Framework

Patreon Twitter Codepen Stackoverflow Slack LinkedIn

About NextPHP

NextPHP is a next-generation, lightweight PHP framework designed for rapid web application development. Optimized for modern cloud environments, it excels in speed, performance, and efficiency, making it the ideal choice for developers aiming to build scalable and secure applications swiftly.

Key Features

Mission

Our mission is to provide a lightweight, easy-to-use PHP framework that helps developers and startups quickly bring their ideas to life. We believe in simplicity, efficiency, and delivering value without unnecessary complexity.

Vision

We envision a world where developers can focus on building innovative solutions without being bogged down by complex setups and configurations. NextPHP aims to be the go-to framework for rapid development and agile methodologies.

Why NextPHP?

In today’s digital transformation era, the speed of development, application performance, and resource efficiency are crucial. NextPHP addresses these needs by offering:

Development History

NextPHP evolved from LeanPHP, NovaPHP, and NGPHP, each bringing unique strengths such as minimalism, optional composer support, and a fully modular architecture. NextPHP combines the strengths of its predecessors with comprehensive support for modern web development needs, optimized for cloud-based projects.

Packages

NextPHP offers a range of packages to enhance functionality and streamline development:

Comparison with Other Frameworks

Unlike some other frameworks that can be cumbersome and resource-intensive, NextPHP is designed to be lean and efficient. This means faster development times, lower resource consumption, and reduced hosting costs.

Feature NextPHP Other Frameworks
Easy Setup & Rapid Development ★★★★★ ★★★
Integrated Core Features ★★★★★ ★★★★
Ease of Learning ★★★★★ ★★★
Performance & Speed ★★★★★ ★★★
Community Support ★★★★ ★★★★★
Ideal for Startups ★★★★★ ★★★
Download & Use Capability ★★★★★ ★★★
Source Code Size ★★★★★ (Kilobytes) ★★ (Megabytes)
RESTful API Support ★★★★★ ★★★★

Unique Advantages of NextPHP

NextPHP Framework Usage Documentation

Overview

The NextPHP Framework is a modern and lightweight PHP framework. Designed for high performance and scalability, it offers a comprehensive set of tools and libraries to accelerate the development process. The framework includes various packages such as NextPHP Rest and NextPHP Data.

Features

Installation

Installation with Composer

To add the NextPHP package to your project, run the following command using Composer:

composer require nextphp/nextphp

Basic Usage

NextPHP comes with an example project demonstrating a full multi-layered architecture CRUD operation with User and Post examples.

Defining Entities

Entities represent the tables in your database. Use attributes to define the properties and their types.

<?php
namespace Example;

use NextPHP\Data\Persistence\Column;
use NextPHP\Data\Persistence\Entity;
use NextPHP\Data\Persistence\GeneratedValue;
use NextPHP\Data\Persistence\Id;
use NextPHP\Data\Persistence\Table;

#[Entity(name: "users")]
class User
{
    #[Id]
    #[Column('INT AUTO_INCREMENT PRIMARY KEY', false)]
    public int $id;

    #[Column('VARCHAR(255)', false)]
    public string $name;

    #[Column('VARCHAR(255)', false)]
    public string $email;

    #[Column('VARCHAR(255)', false)]
    public string $password;

    // getters and setters
}

Advanced Entity Usage

Relationships OneToMany, OnetoOne, ManyToOne and ManytoMany. Define relationships using attributes.

<?php
namespace Example;

use NextPHP\Data\Persistence\Column;
use NextPHP\Data\Persistence\Entity;
use NextPHP\Data\Persistence\GeneratedValue;
use NextPHP\Data\Persistence\Id;
use NextPHP\Data\Persistence\Table;

#[Entity(name: "users")]
class Post
{
    #[Id]
    #[Column('INT AUTO_INCREMENT PRIMARY KEY', false)]
    public int $id;

    #[Column('VARCHAR(255)', false)]
    public string $title;

    #[Column('TEXT', false)]
    public string $content;

    #[Column('INT', false)]
    public int $user_id;

    // example for ManyToMany, OneToMany etc.
    #[ManyToOne(targetEntity: User::class, inversedBy: 'posts')]
    private User $user;

    // getters and setters
}

Using Repository

Repositories handle database operations for entities. Extend BaseRepository and specify the entity class.

<?php
namespace Example;

use NextPHP\Data\BaseRepository;

#[Repository(entityClass: User::class)]
class UserRepository extends BaseRepository
{
    // No need for constructor
}

Service Layer

Services provide business logic and interact with repositories.

<?php
namespace Example;

#[Service(description: 'User management service')]
class UserService
{
    private UserRepository $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    #[Transactional]
    public function registerUser(array $userData): User
    {
        $user = new User();
        $user->name = $userData['name'];
        $user->email = $userData['email'];
        $user->password = password_hash($userData['password'], PASSWORD_DEFAULT);

        $userArray = [
            'name' => $user->name,
            'email' => $user->email,
            'password' => $user->password,
        ];

        $this->userRepository->save($userArray);

        return $user;
    }

    public function getAllUsers(): array
    {
        return $this->userRepository->findAll();
    }

    public function getUserById(int $id): ?User
    {
        $userArray = $this->userRepository->find($id);
        if (!$userArray) {
            return null;
        }

        $user = new User();
        $user->id = $userArray['id'];
        $user->name = $userArray['name'];
        $user->email = $userArray['email'];
        $user->password = $userArray['password'] ?? '';

        return $user;
    }

    public function updateUser(int $id, array $data): ?User
    {
        $user = $this->getUserById($id);
        if (!$user) {
            return null;
        }

        foreach ($data as $key => $value) {
            if (property_exists($user, $key)) {
                $user->$key = $value;
            }
        }

        $userArray = get_object_vars($user);
        $this->userRepository->update($id, $userArray);

        return $user;
    }

    public function deleteUser(int $id): bool
    {
        $user = $this->getUserById($id);
        if (!$user) {
            return false;
        }

        $this->userRepository->delete($id);

        return true;
    }
}

Using Resource

This is an example project demonstrating the usage of the NextPHP Rest package, which includes routing and HTTP handling capabilities. Basic Usage Defining Routes Define routes using attributes to map HTTP methods to controller actions.

<?php

namespace NextPHP\App\Service;

use NextPHP\App\Repository\UserRepository;
use NextPHP\App\Entity\User;
use NextPHP\Data\Service;
use NextPHP\Data\Persistence\Transactional;

#[Service(description: 'User management service')]
class UserService
{
    private UserRepository $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function registerUser(array $userData): User
    {
        $user = new User();
        $user->name = $userData['name'];
        $user->email = $userData['email'];
        $user->password = password_hash($userData['password'], PASSWORD_DEFAULT);

        $userArray = [
            'name' => $user->name,
            'email' => $user->email,
            'password' => $user->password,
        ];

        $this->userRepository->save($userArray);

        return $user;
    }

    public function getAllUsers(): array
    {
        return $this->userRepository->findAll();
    }

    public function getUserById(int $id): ?User
    {
        $userArray = $this->userRepository->find($id);
        if (!$userArray) {
            return null;
        }

        $user = new User();
        $user->id = $userArray['id'];
        $user->name = $userArray['name'];
        $user->email = $userArray['email'];
        $user->password = $userArray['password'] ?? '';

        return $user;
    }

    public function updateUser(int $id, array $data): ?User
    {
        $user = $this->getUserById($id);
        if (!$user) {
            return null;
        }

        foreach ($data as $key => $value) {
            if (property_exists($user, $key)) {
                $user->$key = $value;
            }
        }

        $userArray = get_object_vars($user);
        $this->userRepository->update($id, $userArray);

        return $user;
    }

    public function patchUser(int $id, array $data)
    {
        $user = $this->getUserById($id);
        if (!$user) {
            return null;
        }

        $this->userRepository->save($user);

        return $user;
    }

    public function deleteUser(int $id): bool
    {
        $user = $this->getUserById($id);
        if (!$user) {
            return false;
        }

        $this->userRepository->delete($id);

        return true;
    }
}

Generate the index.php file

To start the NextPHP package, you can create an index.php file and use the service layer to perform various CRUD operations. Here is an example of how you can do this:

Example index or example.php

<?php

require_once __DIR__ . '/vendor/autoload.php';

use NextPHP\Rest\DI\Container;
use NextPHP\Rest\Router;
use NextPHP\Rest\Http\Request;
use NextPHP\Rest\Http\Response;
use NextPHP\App\Resource\UserResource;
use NextPHP\App\Resource\PostResource;

$container = new Container();

$router = new Router([
    'baseUri' => '/nextphp-beta',
    'allowedOrigins' => [
        'http://allowed-origin.com' => ['GET', 'POST'],
        'http://another-allowed-origin.com' => ['GET', 'PUT'],
        '*' => ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'TRACE', 'CONNECT', 'PRI']
    ]
], $container);

// DI
$router->registerRoutesFromController(UserResource::class);
$router->registerRoutesFromController(PostResource::class);

$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];

$request = new Request($method, $uri, getallheaders(), file_get_contents('php://input'), $_GET, $_POST);
$response = new Response();

$response = $router->dispatch($request, $response);

if ($response) {
    http_response_code($response->getStatusCode());
    foreach ($response->getHeaders() as $name => $value) {
        header("$name: $value");
    }
    echo $response->getBody();
} else {
    http_response_code(500);
    echo json_encode(['error' => 'Internal Server Error', 'message' => 'No response returned.']);
}

Project Architecture

Example for your Project Structure

example/
├── src/
│   ├── Entity/
│   │   ├── User.php
│   │   ├── Post.php
│   ├── Repository/
│   │   ├── UserRepository.php
│   │   ├── PostRepository.php
│   ├── Service/
│   │   ├── UserService.php
│   │   ├── PostService.php
│   ├── Resource/
│   │   ├── UserResource.php
│   │   ├── PostResource.php
├── index.php
├── composer.json
└── README.md

Contributing

We welcome contributions! Here’s how you can help:

For more details, see our Contribution Guidelines.

Resources

Join Our Community

Contact Us

Thank you for being part of the NextPHP community!