ravikisha/nexaphp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ravikisha/nexaphp

Composer 安装命令:

composer require ravikisha/nexaphp

包简介

NexaPHP is a lightweight and flexible MVC (Model-View-Controller) framework for PHP, designed to streamline the development of web applications. With NexaPHP, you can quickly build scalable and maintainable PHP applications by separating concerns and promoting code organization.

README 文档

README

NexaPHP

NexaPHP

PHP Version Version License Status

NexaPHP is a PHP MVC framework designed to provide a lightweight and flexible structure for building web applications. This document provides a detailed guide to understanding and utilizing the framework.

Table of Contents

1. Installation

Install NexaPHP via Composer:

composer require ravikisha/nexaphp

2. Setup

To start, initialize the Application class with the root directory and configuration:

use ravikisha\nexaphp\Application;

$config = [
    'userClass' => \app\models\User::class,
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=testdb',
        'user' => 'root',
        'password' => 'password'
    ]
];

$app = new Application(__DIR__, $config);
  • userClass: Defines the User model.
  • db: Database configuration parameters (dsn, user, password).

3. Core Components

The NexaPHP framework includes several core components:

  • Application: The main entry point to the application.
  • Router: Manages route handling.
  • Request: Handles HTTP requests.
  • Response: Sends HTTP responses.
  • Database: Manages database interactions.
  • Session: Manages session data.

NexaPHP Architecture

4. Controllers

Controllers in NexaPHP define how to handle different routes.

namespace app\controllers;

use ravikisha\nexaphp\Controller;

class SiteController extends Controller
{
    public function home()
    {
        return $this->render('home');
    }

    public function contact()
    {
        return $this->render('contact');
    }
}
  • Controller::render(): Renders a view.
  • setLayout(): Sets a custom layout.

5. Routing

Define routes using Router::get() for GET requests and Router::post() for POST requests:

$app->router->get('/', [SiteController::class, 'home']);
$app->router->post('/contact', [SiteController::class, 'contact']);

NexaPHP supports dynamic route parameters:

$app->router->get('/profile/{id}', [UserController::class, 'profile']);
  • Route parameters can be accessed using Request::getRouteParams().

6. Database Integration

NexaPHP integrates with PDO for database management.

  1. Define a Model:

    namespace app\models;
    
    use ravikisha\nexaphp\db\DBModel;
    
    class User extends DBModel
    {
        public string $id;
        public string $name;
    
        public static function tableName(): string
        {
            return 'users';
        }
    
        public function attributes(): array
        {
            return ['id', 'name'];
        }
    }
  2. Migrations: Run migrations to manage the database structure.

    $app->db->applyMigrations();
  3. Querying:

    • save(): Save a model instance.
    • findOne(): Retrieve a record by criteria.

7. Middleware

Middleware allows filtering and controlling request handling.

  1. Create Middleware:

    namespace app\middlewares;
    
    use ravikisha\nexaphp\middlewares\BaseMiddleware;
    
    class AuthMiddleware extends BaseMiddleware
    {
        public function execute()
        {
            // Logic for authentication
        }
    }
  2. Apply Middleware:

    $this->registerMiddleware(new AuthMiddleware(['profile', 'settings']));

8. Views

Views define how content is rendered. The View class handles view rendering.

// Render a view file located in views directory
return $this->render('viewName', ['param' => $value]);
  1. Layouts:

    • Define layout files under views/layouts.
    • Use {{content}} to insert view content.
  2. Passing Parameters:

    return $this->render('profile', ['name' => 'John Doe']);

9. Forms and Fields

Forms and fields are handled through classes in ravikisha\nexaphp\form.

  1. Form:

    use ravikisha\nexaphp\form\Form;
    
    $form = Form::begin('/submit', 'post');
    echo $form->field($model, 'username');
    Form::end();
  2. Field Types:

    echo (new Field($model, 'password'))->passwordField();

    Supported types: password, email, number, date, file, etc.

10. Session Management

The Session class provides functions for handling sessions.

  • setFlash(): Set flash messages.
  • getFlash(): Retrieve flash messages.
  • set() and get(): Manage session data.

Example:

Application::$app->session->setFlash('success', 'Logged in successfully');

11. Exception Handling

The framework includes custom exceptions:

  • NotFoundException: Triggered for invalid routes.
  • ForbiddenException: Used for access control.

12. User Authentication

UserModel is an abstract class that provides basic functionality for user management.

class User extends UserModel
{
    public static function primaryKey(): string
    {
        return 'id';
    }

    public function getDisplayName(): string
    {
        return $this->username;
    }
}
  • login(): Log in a user.
  • logout(): Log out a user.
  • isGuest(): Check if a user is logged in.

13. Events

The Application class supports custom events.

  1. Define an Event:

    Application::$app->on(Application::EVENT_BEFORE_REQUEST, function () {
        // Before request logic
    });
  2. Trigger Events:

    Use triggerEvent() to initiate custom behavior at various points in the application.

14. Sample Application

Below is an example of setting up a simple application using NexaPHP:

// Load NexaPHP classes
require_once __DIR__ . '/vendor/autoload.php';

use ravikisha\nexaphp\Application;
use app\controllers\SiteController;

$config = [
    'userClass' => \app\models\User::class,
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=testdb',
        'user' => 'root',
        'password' => 'password'
    ]
];

// Initialize the application
$app = new Application(__DIR__, $config);

// Define routes
$app->router->get('/', [SiteController::class, 'home']);
$app->router->get('/contact', [SiteController::class, 'contact']);

// Run the application
$app->run();

This Project in for the purpose of learning and understanding the MVC architecture and how it works. It is not intended to be used in production environments. For production, it is recommended to use a well-established framework like Laravel, Symfony, or Yii.

License

This Project is licensed under the MIT License.

Open Source

This Project is open source and contributions are welcome. Feel free to fork and submit a pull request. For major changes, please open an issue first to discuss what you would like to change.

ravikisha/nexaphp 适用场景与选型建议

ravikisha/nexaphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「database」 「orm」 「routing」 「php」 「json」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ravikisha/nexaphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ravikisha/nexaphp 我们能提供哪些服务?
定制开发 / 二次开发

基于 ravikisha/nexaphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-11