devhammed/server-actions
Composer 安装命令:
composer require devhammed/server-actions
包简介
PHP server actions!
README 文档
README
Making Next.js Server Actions work in PHP!
This is a proof of concept inspired by this tweet, and it's not meant to be used in production.
But it works so let's go!
Installation
The recommended way to install this package is through Composer.
composer require devhammed/server-actions
Then include the Composer autoloader in your entry file (e.g index.php) like:
<?php // File: /public/index.php require_once __DIR__ . '/../vendor/autoload.php';
But you can also download this repository and include the provided autoload.php file e.g
require_once __DIR__ . '/libs/server-actions/autoload.php';
Usage
You can use this package in any PHP project, but it has first-class support for Laravel and all the heavy lifting is done for you.
You can skip to the Laravel section if you are using Laravel.
The basic concept is that you have to initialize the Server in your entry file (e.g index.php) before using it by calling the useServer() function
without any arguments which will return the instance that you can use to configure the server endpoint and the storage for the server actions which the package currently provides
JsonFileServerEntry that stores serialized actions in a JSON file, you can implement DevHammed\ServerActions\Contracts\ServerEntry interface.
<?php // File: /public/index.php require_once __DIR__ . '/vendor/autoload.php'; use DevHammed\ServerActions\Concerns\JsonFileServerEntry; use function DevHammed\ServerActions\useServer; useServer() ->withServerActionsUrl('/server-actions.php') ->withServerEntry( new JsonFileServerEntry( __DIR__ . '/server-actions.json', ), ); // Other logic to include your PHP files for the request here...
Then you need to create the server actions handler file that was specified in the withServerActionsUrl method e.g server-actions.php that will look
almost the same as the entry file but with the run() method called at the end of the chain and nothing else, like:
<?php // File: /public/server-actions.php require __DIR__ . '/../vendor/autoload.php'; use DevHammed\ServerActions\Concerns\JsonFileServerEntry; use function DevHammed\ServerActions\useServer; useServer() ->withServerActionsUrl('/server-actions.php') ->withServerEntry( new JsonFileServerEntry( __DIR__ . '/server-actions.json', ), ) ->run();
Then you can use the useServer helper function in the included PHP files like:
<?php // File: /views/my-form.php use function DevHammed\ServerActions\useServer; ?> <form method="post" action="<?= useServer(function (string $name) { echo 'Hello, ' . $name; }) ?>" > <input type="hidden" name="name" value="Hammed"> <button type="submit">Greet the creator</button> </form>
Laravel
This package provides a service provider for Laravel that will automatically initialize the server and endpoint for you.
You can register the service provider in your config/app.php file if you are using Laravel 5.4 or below, but if you are using Laravel 5.5 or above, the package will automatically register itself using auto-discovery.
<?php // File: config/app.php return [ // ... 'providers' => [ // ... DevHammed\ServerActions\ServerActionsProvider::class, ], // ... ];
After that, you should run the following command to publish the configuration file to config/server-actions.php and setup other things that might be needed.
php artisan server-actions:install
Then you can use the useServer helper function in your Blade templates like:
<?php // File: /resources/views/my-form.blade.php use function DevHammed\ServerActions\useServer; ?> <form method="post" action="<?= useServer(function (string $name) { return 'Hello, ' . $name; }) ?>" > @csrf <input type="hidden" name="name" value="Hammed"> <button type="submit">Greet the creator</button> </form>
Note that the @csrf directive is required for Laravel to accept the request since this is like
every other form request and the handler can be used just like you would use a controller method e.g redirecting, returning a view, etc.
which is why we are returning a string instead of echoing it unlike the vanilla PHP example.
That's it! Go make some server actions!
License
This package is open-sourced software licensed under the MIT license.
Credits
devhammed/server-actions 适用场景与选型建议
devhammed/server-actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 10 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「server」 「actions」 「php server」 「nextjs」 「server actions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devhammed/server-actions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devhammed/server-actions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devhammed/server-actions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
Turns CPD style XML reports into Github pull-request annotations via the Checks API. This script is meant for use within your Github Action.
Action for small code bits, usable in laravel
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-28