eru123/wyue

在PHP之上开发Vite.js应用程序的框架

dev-main 2024-09-26 11:46 UTC

This package is auto-updated.

Last update: 2024-09-26 11:46:47 UTC


README

在PHP之上开发Vite.js应用程序的框架

在我的最近的项目中,我使用PHP作为API和Vue 3与Vite在客户端开发不同的应用程序,我成功地将这些技术结合到一个项目中,甚至与热模块替换(HMR)一起用于开发,并添加了元数据到我的Vue应用程序以改善搜索引擎优化(SEO)。当我开始新的项目时,我复制了之前项目的代码并用作我的样板,然后添加了新的功能和功能,然后我也更新了其他旧项目以修复错误和添加来自其他项目的新的功能,这在某种程度上是我的混乱和耗时的,所以我决定创建这个框架来使我的生活更容易、更有组织。由于这个项目帮助我构建项目,我希望有人也能使用它并尝试一下,如果你喜欢它,你也可以给它留下星标或资助这个项目,我同样感激任何反馈。

特性

路由

要开始路由,你必须首先使用Route命名空间

use Wyue\Route;

要了解路由处理程序和中间件,这里是如何定义处理程序函数的一个示例

$handler = function (Route $context, $result = null) {
    // process handler
}

对于响应,你可以使用code方法设置HTTP响应状态码

$handler = function (Route $context, $result = null) {
    $context->code(202);
    return 'OK'; // response with html/text
}

将处理程序视为数据管道,其中前一个的结果传递给下一个

$analyticsHandler = function (Route $context, $result = null) {
    // does not return anything, just processing some background tasks
}

$authHandler = function (Route $context, $result = null) {
    // for the example, we assume that the request failed to send valid credentials so we want to invalidate it
    // if you return false in a handler it will stop processing the next handlers and proceed to process the next routes
    return false;

    // Alternatively, you can just throw an exception with http error codes
    throw new Exception("Forbidden", 403);

    // You could also return a json response by returning an array
    $context->code(403);
    return [
        "code" => 403,
        "error" => "Forbidden",
        "message" => "Please login to access!",
    ];
}

$apiHandler = function (Route $context, $result = null) {
    // since you return false from previous handler, you will not reach this code
}

$route = new Route();
$route->Route('/api/users', $analyticsHandler, $authHandler, $apiHandler);
$route->Route('/api/user/2', $analyticsHandler, $authHandler, $apiHandler);
$route->Route('/api/user/3', $analyticsHandler, $authHandler, $apiHandler);
$route->Route('/api/user/4', $analyticsHandler, $authHandler, $apiHandler);

使用Route方法允许你在不受请求方法限制的情况下处理路由,要限制请求方法,可以使用以下函数:GetPostPutPatchDelete

$route = new Route();
$route->Get('/api/user', $analyticsHandler, $apiHandler);
$route->Post('/api/user', $analyticsHandler, $authHandler, $apiHandler);

如果你熟悉Laravel或Codeigniter,它们有一些调用路由处理程序函数的魔法,我以某种方式实现了它们调用路由处理程序函数的方式。

class AuthController {
    public function login(Route $context, $result = null) {
        // process login here
    }

    // ...
}

function loginFunction(Route $context, $result = null) {
    // process login here
}

// Different ways to call route handler functions
$auth = new AuthController();
$route->Post('/login', [$auth, 'login']); // I believe this is the most recommended way for performance reasons
$route->Post('/login', [AuthController::class, 'login']);
$route->Post('/login', 'AuthController@login');
$route->Post('/login', 'AuthController::login');
$route->Post('/login', 'loginFunction');

使用Wyue,你也可以以不同的方式定义URL参数,并使用Params方法从上下文中获取其值

// actual url path: /api/user/1

class UserController {
    public function user(Route $context, $result = null) {
        // process user here
        $id = $context->Params('id', 'default value if id not exist in url');
        // or 
        $id = $context->Params()['id'];
    }
}

$route->Get('/api/user/{id}', 'UserController::user');
$route->Get('/api/user/$id', 'UserController::user');
$route->Get('/api/user/:id', 'UserController::user');

如果你想以更复杂的方式定义你的路由,尝试在处理程序函数内部创建一个新的路由以实现嵌套路由

$route->Route('/api', function (Route $route) {
    $route->Route('/user/$id', function (Route $route) {
        $route->Delete('/delete', function (Route $route) {
            // delete user
        })

        $route->Put('/update', function (Route $route) {
            // update user
        })

        return [
            // ... user details
        ];
    });

    $route->Get('/users', function (Route $route) {
        // list users
    });

    return [
        // ... fallback
    ];
})