https://github.com/devhammed/php-server-actions
Next.js Server Actions but for PHP!
https://github.com/devhammed/php-server-actions
composer-package laravel laravel-package nextjs nextjs13 php
Last synced: 3 months ago
JSON representation
Next.js Server Actions but for PHP!
- Host: GitHub
- URL: https://github.com/devhammed/php-server-actions
- Owner: devhammed
- License: mit
- Created: 2023-10-28T08:50:34.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-29T06:47:48.000Z (over 2 years ago)
- Last Synced: 2025-01-20T15:17:17.549Z (about 1 year ago)
- Topics: composer-package, laravel, laravel-package, nextjs, nextjs13, php
- Language: PHP
- Homepage: https://packagist.org/packages/devhammed/server-actions
- Size: 34.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# devhammed/server-actions
[](https://packagist.org/packages/devhammed/server-actions)
Making [Next.js Server Actions](https://nextjs.org/docs/app/api-reference/functions/server-actions) work in PHP!
This is a proof of concept inspired by [this tweet](https://x.com/WebReflection/status/1717853489034932631?s=20), 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](https://getcomposer.org/).
```bash
composer require devhammed/server-actions
```
Then include the Composer autoloader in your entry file (e.g `index.php`) like:
```php
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
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
Greet the creator
```
### 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
[
// ...
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.
```bash
php artisan server-actions:install
```
Then you can use the `useServer` helper function in your Blade templates like:
```php
@csrf
Greet the creator
```
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](LICENSE.md).
## Credits
- [Hammed Oyedele](https://github.com/devhammed)