npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-sveltekit-php-backend

v0.6.0

Published

PHP Backend Vite Plugin for SvelteKit

Downloads

3

Readme

PHP Backend for SvelteKit

You can have PHP logic directly in SvelteKit route directory like this: +page.server.php

<?php
function load($event) {
    $name = ($event->params['name'] ?? 'unknownn');
    return [
        'message' => "Hello " . $name,
    ];
}

You can also use PHP to write form actions or endpoint.

Because SvelteKit talks Web standards, communication between SvelteKit and backend PHP server is pretty straight forward using HTTP semantics.

  • This plugin allows you to use SvelteKit the frontend of PHP backend.
  • Not like the one which uses SvelteKit as an assets manager.
  • No need to have a router in PHP side. SvelteKit is the router.
  • No need to have Nginx as a front end of PHP-fpm. Plugin communicate directly with PHP-fpm.
  • Of course, it's Composer compatible so that you can use any composer PHP packages including your own application logic.
  • It is obvious but you cannot create +page.php because there's no PHP runtime in client environment.

How to run demo

Prerequirements:

  • PHP with PHP fpm running
  • composer for PHP packages
  • Node.js

Steps

  1. cd to example/
  2. yarn to install required packages.
  3. yarn dev --open to launch dev server.

Try pages and take a look into the code in src/routes

src/routes/php/[fruite]

Example of page server load. The route parameters are passed to +page.server.php. Also it returns $_SERVER php variables for reference.

src/routes/php/emoji/[fruite]

Example of endpoing.

src/routes/pherdle

Example of form actions. Ported Sverdle to PHP.

src/php

PHP library bound to App\ namespace. You can place application logic in this folder. See composer.json for the configuration.

Motivation of This Project

You might ask, "Who uses PHP in 2023?"

Well, I do. And many projects in the world are using PHP by various reasons. I want to use modern client technology and I can use Svelte component in our app easiliy.

But not for SvelteKit. It's a complete web application framework and that's overlaps with the area PHP apps covers. As long as using PHP's routing feature, we cannot integrate SvelteKit's full power, such as unclear border between client and server, or prerendering during the build time.

With this plugin, you can connect your business logics written in PHP directly to SvelteKit's page logic. You can replace following code from PHP to SvelteKit:

  • router
  • controller
  • page rendering

No nginx required any more

Usually your PHP application is served via PHP-fpm. Because PHP-fpm doesn't talk HTTP by itself, you usually place nginx in front of PHP-fpm. This plugin uses fastcgi-kit npm package (which is written by me for this project :p ) to communicate directly with PHP-fpm.

On development phase, there's no need to launch PHP dev server any more. Just launch PHP-fpm service and any script will run from SvelteKit route. (Well, of course with under the restriction of PHP-fpm security).

On production, you can replace nginx with node application (might be Express.js) to run SvelteKit application and every PHP call will be handled via FastCGI directly from the SvelteKit's page logic.

Building will be supported in the next version.

Frontend is easier to replace than backend

Suppose your codebase has been running for decades. Your app uses PHP for a historical reason. You are very passionate to make the application modernized. What can you do?

Simple application is easy. You can rewrite the app from the scratch. But decent size of application includes many business logics in the code.

Frontend trends such as user interface changes quickly but your businness logic isn't. At lease those changes should be unrelated to the frontend trends.

License

MIT