Download the PHP package fusionphp/fusion without Composer
On this page you can find all versions of the php package fusionphp/fusion. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download fusionphp/fusion
More information about fusionphp/fusion
Files in fusionphp/fusion
Download fusionphp/fusion
More information about fusionphp/fusion
Files in fusionphp/fusion
Vendor fusionphp
Package fusion
Short Description A Laravel package to bridge the gap between Laravel and Vue/React.
License MIT
Package fusion
Short Description A Laravel package to bridge the gap between Laravel and Vue/React.
License MIT
Please rate this library. Is it a good library?
Informations about the package fusion
Unite your modern frontend with your Laravel backend.
--- Fusion is the best way to combine your Laravel backend with your JavaScript frontend. We currently only support Vue.js, but React + Svelte are on the roadmap. > [!CAUTION] > 🚨🚨🚨 > > Fusion is in a _very_ early development preview. Please do not use it in production yet! There are still lots of > bugs (probably.) > > 🚨🚨🚨 ## Concepts It is important to note up front what Fusion **does not** do: - Fusion does not transpile your PHP to WASM. - Fusion does not turn your JavaScript into PHP/Blade. - Fusion does not use the "PHP for templating." - Fusion does not *automatically* sync frontend/backend state. What Fusion **does** do: - Fusion uses Vite to extract the PHP blocks from your JavaScript files and writes the PHP to disk. - Fusion uses Vite to inject some information into your JavaScript file as it is being transpiled. - Fusion runs your PHP on the backend and your JavaScript on the frontend. - Fusion turns your PHP block into a sort of controller. - Fusion uses the standard Laravel request/response lifecycle, router, auth, middleware, etc. - Fusion allows *you* to sync frontend/backend state. Conceptually, you can think of the `
From that menu, choose "XML Tag Injection".
Then enter the following details.
This should make your editing experience much nicer. We'll work with the JetBrains team to make this step unnecessary.
As of now, we have no instructions on VSCode. Please stay tuned, we'll figure it out as quickly as we can.
## State
One of Fusion's primary responsibilities is to send your state to the frontend. State is sent at runtime, not compiled
into your JavaScript bundle. The only thing that gets written into your bundle are the names of your properties and
exposed actions.
### State with Procedural PHP
When using procedural PHP, you can expose a variable to the frontend by using the `Fusion\prop` function.
> [!TIP]
> This is very non-traditional PHP, and only possible because we're transpiling your PHP code before it gets written to
> disk. All transpiled PHP is written into your storage directory and you're free to inspect it!
By assigning a variable of `$name` to the function `prop`, you've alerted Fusion that `$name` is something that should
be shared with the frontend.
#### Defaults
You are free to pass in a default value as either a scalar or a Closure.
The _default_ is "Aaron", but if a different value is sent from the frontend, the `$name` variable will be assigned to
that value. If the frontend sends `name: "Steve"`, then in your PHP `$name = "Steve"` instead of `"Aaron"`.
We'll cover syncing state further down.
#### Tracking
Something that may feel familiar to a user of a JavaScript framework is that we'll keep track of that variable
throughout your code. This will feel strange to most PHP developers:
We're only able to do this because we are transpiling your code. At the end of the code that you write, we call a Fusion
method named `syncProps` passing in `get_defined_vars()`.
This gives us the names and values of all of the variables that are in scope. If you've declared a variable as a prop,
we will take the last value and use that as the state for the frontend.
#### Readonly state
To declare a piece of state "readonly", meaning that you never want to receive it back from the frontend, you can append
`->readonly()` to the `prop` function.
When `name` gets sent to the frontend, it can still be modified *on the frontend,* if you allow that. But the frontend
will never send it to the backend, the backend will *always* recalculate it.
You can think of this type of state as computed props.
#### Syncing values to the querystring
It may be convenient to sync state to the querystring to create stable URLs. You may do so by appending
`->syncQueryString`.
By default `$search` is null, but if there is a `?search=` in the URL, that value will be used. If you want the
querystring name to be different than the variable name, you may use the `as: ` argument.
The querystring will now use `?s=` to track this prop.
### State with Class-based PHP
Using class-based PHP, any properties that are `public` will be sent to the frontend. (WIth one caveat, mentioned
below.)
Declaring a class like this:
will send `name` to your frontend to be consumed.
If you need to hide a `public` property, you may annotate it with `#[Fusion\Attributes\ServerOnly]`. You should rarely
need this. If you find yourself needing this often, please open an issue and explain why! We might be able to make it
less cumbersome.
#### Readonly state
You may annotate a public property with `#[Fusion\Attributes\IsReadOnly]` to mark a property as readonly, i.e., it will
never be set from a frontend request.
Alternatively, you may set the value via the `mount` method to accomplish the same outcome.
Consider the previous class:
The default for `$name` is `Aaron`, but if a value comes in from the frontend where `name: "Steve"` then the `$name`
variable will be set to `Steve`.
However, if you annotate it with the attribute, it will stay "Aaron".
Beyond annotating with the `#[IsReadOnly]` attribute, you can just brute-force the value in `mount`:
Now, regardless of what was sent from the frontend, we set the value to `"Aaron"`. You could accomplish a "default" by
using the null coalescing operator:
This allows you to potentially sync the `name` state from the frontend, but otherwise assign a default.
#### Syncing QueryString
If you'd like to sync a property to the querystring, you may use the `#[SyncQueryString]` attribute.
You're free to pass an `as:` argument to control the name of the querystring:
The variable passed to your frontend will be named `search`, but in the querystring it will appear as `s`.
## Actions
On the backend, along with state, you can declare "actions." Actions are a way for the JavaScript frontend to reach over
the network and call functions on your backend. In the class-based approach, any method that you define as a `public`
method will be exposed to the frontend.
We'll talk further down about calling these methods from your JavaScript, but now on the frontend you have a `hello`
function that will route to this method on the backend.
If, for whatever reason, you don't want a public method to be reachable from the frontend, you can add a `ServerOnly`
attribute to it in class-based PHP.
In procedural PHP, you may pass the function as a named argument to the `expose` method and that will make it visible to
the frontend.
## Working with JavaScript
You don't _have_ to do anything to receive your state on the frontend, but there is a lot you _can_ do. Because we have
a
plugin in the Vite toolchain, we're able to add some Vue code to inject the state and actions for you automatically,
provided you don't do it yourself.
The automatic process is this simple:
That's it! You do not have to define props, a `script` tag of any sort, or anything else.
> [!NOTE]
> We are still working on making your editor aware of these injected pieces of data. More on that soon.
If you'd like to define your own script tags, you have as many options as Vue supports.
### A `