---
title: I built a way to write PHP alongside your frontend
url: "https://aaronfrancis.com/videos/i-built-a-way-to-write-php-alongside-your-frontend"
path: "/videos/i-built-a-way-to-write-php-alongside-your-frontend"
type: video
author: Aaron Francis
published: 2025-02-04
topic: "Laravel & PHP"
keywords: [fusion, php in javascript, laravel javascript, file-based routing, route model binding, reactive props, state syncing, vue, react, type safety, two-way data flow, php laravel]
youtube_url: "https://www.youtube.com/watch?v=sa3XHjG1Kgs"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# I built a way to write PHP alongside your frontend

## Chapters

- Mind-Blowing PHP in Javascript! 🤯
- Writing PHP Inside a Javascript Component?!
- What is Fusion? A Laravel + JavaScript Revolution
- No Routes, No Controllers—Just Magic ✨
- File-Based Routing & Route Model Binding
- Reactive Props and State Syncing
- Full Laravel Database Queries in Vue
- Automatic Type Safety (Coming Soon!)
- Seamless Error Handling for PHP & Vue
- Real-Time Search with Two-Way Data Flow
- Fusion's Built-In Loading & Success States
- Exposing PHP Functions Directly to Vue
- Alternative Syntax for PHP Purists
- The Future of Fusion: React, Svelte & More
- How to Get Involved & Support Fusion

## Transcript

**Aaron Francis:** Are you ready to have your mind blown? Look at this. This is a view component. We've got hello over here. It shows up hello over there, and if we say world, we get a little bit of hot module reloading. That's not the impressive part, okay? Everybody knows that. That's not impressive. We're gonna remove the script tag, and instead of having a script tag, we're gonna have a PHP tag. Yes, we know it. We love it.

**Aaron Francis:** It's PHP. So if we put, uh, hello name, and we just put a little prop in there, we can come down here and access that name. Now, nothing's gonna show up, except we did get hot module reloading, which is fun. And if I were to type in Aaron and do that again, from PHP, the variable is coming down.

**Aaron Francis:** It is usable in your view component, and you get hot module reloading as well. So what the heck is, is going on here? Did we just write PHP in a view component? You bet we did. We wrote PHP in a view component, and then we sent that state down to the front end. We can do functions as well. We can have functions defined on the back end, and your front end can just reach directly into the back end of that application and run those functions with no routes, no controllers, no nothing.

**Aaron Francis:** It's just available to you. We're writing PHP and JavaScript in the same file. This is Fusion. This is a library that I have been working on that brings your Laravel back end and your JavaScript front end much closer together. You c- it, you can even put it in the same file. So I'm gonna show you a bunch of stuff that is going on here. Um, it is important that I say up front, we're not running PHP on the front end.

**Aaron Francis:** This is not some WASM thing where I'm compiling it and sending it to the front end. What this is doing is this is papering over that network gap. So I'm defining a bunch of stuff on the back end, and then we're using Vite to, um, take that and transform it into something that the front end can understand. And then we, as Fusion, will handle making sure that those two can talk together.

**Aaron Francis:** So it's still traditional request response. We're not doing anything wonky like running PHP on the front end. But let me show you some more stuff we can do. The first thing I wanna show you is how we're routing to these, uh, components. This still uses inertia under the hood, so we're still gonna use the concept of inertia pages. But instead of having to define them one by one, we're just gonna say fusion path, and then anything in this path, um, is going to show up as a route.

**Aaron Francis:** And so over here, if we take a look at pages, we have hello there. Now, if I were to copy this and put, uh, let's just name it hi, and then we change this to hi, and then we come over here. We'll make this big, and instead of going to hello, we can go to hi, and it's just going to work. So we have, uh, file-based routing.

**Aaron Francis:** Now, with file-based routing, you're maybe wondering, "Well, how do I get route model binding? Like, what if I have a podcast?" That's totally fine. We took care of that as well. Um, so you can do, uh, route model binding like that, and I'll show you how to define this stuff in a little bit. And if you have kind of a catchall route, you can do, uh, a variadic parameter like that.

**Aaron Francis:** So we've got file-based routing. You've got route model binding, path segments, all of that kind of stuff. You can wrap a route group around your fusion route. So if you wanna put them on a subdomain or behind a certain middleware, you can totally do that. You don't have to, but it is available to you. Let me show you a few more things we can do with state, sending it down, but then we're gonna start sending some state back and forth.

**Aaron Francis:** We're gonna sync the two together. Coming back to this hello.view, um, I'm gonna do something that you don't necessarily have to do, but it is kind of nice. I'm going to import this function prop. So this, uh, function is globally available, but this gives my editor a little bit of help, and it lets me auto-complete. I can sync query string.

**Aaron Francis:** So if we open that up, you see that name is now up here in the query string, and if I change it to Aaron, it does get synced on down. And that is because we're saying, "Hey, the default here is world." We're gonna start with world, but pull anything in from the query string. And so this can be really nice for, like, uh, search parameters, uh, number of items per page, that sort of thing. Now, let's go ahead and drop this off.

**Aaron Francis:** I think something that, um, PHP developers might not expect here is this is, uh, this is reactive. So if I change that to, uh, from world to Aaron, it is reactive. So what, what we do is we keep track of we noticed that you declared this as a prop, and so we keep track of it through the life cycle of your PHP code.

**Aaron Francis:** And then at the end, we say, "All right. What's the final state?" Because that lets you do stuff that you would, you know, want to do in PHP, which is like, I don't know, do some programming. And so if you change that variable, we'll keep track of it, no problem. Now, I showed you, or rather I told you, that we had route model binding. So we're gonna hop over to this, um, podcast in brackets page, and this defines a, uh, a route parameter.

**Aaron Francis:** So instead of hello, and we're not nested here, so we're just gonna type in, um, we're just gonna type in one. So we're still on hello world. I wonder if this worked. Let's change this to podcast, and we'll change this down here to podcast. And so prop world, yep, that's still the same thing. Now, we don't actually need to define anything here because what we wanna do is we wanna pull it out of the route.

**Aaron Francis:** So we can say from route, and if we pull that down, hello one. Hey, that's pretty cool. So we pulled that down. Um, this follows all of Laravel's route model binding, so this is using, uh, route model binding under the hood. Now, um, we're using route model binding, but it's really just route binding 'cause we don't have any models yet.

**Aaron Francis:** So if we do this, we change this from podcast to podcast.title, we'll see some console logs because that is, uh, an integer. We can say class is, and then I believe it is app models, uh, podcast. Class. And so let's make a little bit of space, working on a tiny little screen here.

**Aaron Francis:** So if we do that, Art of Product. Look at that. So we're pulling down... We can just, you know, let's just see the whole thing. Why not? We can see the whole podcast here that we're pulling down out of the database, and if we change this to two, it changes to Bootstrapped Web. So now we're doing route model binding. We're pulling a, um, we're pulling an ID out of the route.

**Aaron Francis:** We're looking it up in the database using Laravel. I, I, I do very little. Laravel does all the work here. It looks it up in the database, resolves it, and then gives it back to us. And you may be thinking, boy, we're just, we're just throwing data around willy-nilly, right? First it was an integer of the ID, and now it's an entire podcast object. I hear you. In the process, in the process of, um, the Vite plugin doing its work, it writes a JavaScript shim file next to your Vue, uh, your Vue file.

**Aaron Francis:** So in that file, we have a bunch of JSDoc annotations. And so very, very soon, we will have full complete type safety from end to end, so you'll be able to type podcast, and if you misspell it, your editor will tell you, "That variable doesn't exist." And if you hit dot, then you'll see autocomplete for title, author, image. I got it. We're gonna get there. It's totally gonna work. I wanna show you one more thing before we do a full two-way sync, and that is a little bit of the safety that is built in on the PHP side.

**Aaron Francis:** So if you are, um, if you're in here, let's just say we're going back just to podcast, and that's going to be just an empty prop. In fact, we can say, um, we'll just type podcast so we know that it is working. Now, we're back here in a Vue template, but we're writing PHP, and so what happens if you write bad PHP? Also, I got you.

**Aaron Francis:** It's totally fine. So over here, we get PHP parse error coming from Vite, um, and it says unexpected end of file where the PHP tag closes, and that is because I forgot this semicolon. Now we're back in business. So a lot of work has gone into making this, uh, developer experience as, um, seamless as can be, even when it comes to errors.

**Aaron Francis:** Now, let's do a round trip. Let's do something cool. Um, we've got a podcast search page, and we've got podcasts as an empty array, search as an empty string, and loading as false. We gotta get some data down. We gotta get some actions back. Let's see how this can work. Now, first things first, let's come down here to the input, and let's just put, yeah, right up here we can put a, uh, v-model equals search on this.

**Aaron Francis:** Now, when we send this data down, it's just a, it's just a Vue app at that point. Uh, it's just a React app at that point. The data is down there. When we v-model to it, I'm just syncing that locally. I'm not gonna sync that back to the server. You're in control of that. So y- every key press isn't gonna send an event back home.

**Aaron Francis:** We sent the data down. You do with, do with it what you want and send it back to us if you can. So if we do this and we change this to, um, we're gonna search for Aaron, and then over here we see Aaron pop into the box. So that is working. That's great. Where are we gonna get these podcasts? Well, we can, um, we can pass a function here, and we can say return podcast, um, I think it's just all.

**Aaron Francis:** I think that's all you have to do. So if we return those podcasts, now we're getting those podcasts sent down, and we're v-foring over them, uh, down here, podcast and podcast, just loop over them. So how do we wire up, how do we wire up this search button? Well, it's pretty easy. Um, let's first imagine...

**Aaron Francis:** Let's actually, we can just type it in. What's the first one? Art. So we're gonna type in art there, and so now search is art, but we're not actually using that, so we need to modify our PHP a little bit so we can use search. I do, um, I do change your PHP code in the Vite process, but I don't fundamentally change it, so we can still just use search like you, like you normally would.

**Aaron Francis:** And we can say if search, then, um, we'll return podcasts, uh, query. What do we do? Um, where title is like, and then we'll throw some of those guys on it, and then we'll say search and get. So if we return all of those, now we see that just art is coming back.

**Aaron Francis:** So our search is working, but, I mean, is it? Is it? Is it working? No, of course it's not. So what do we do here? What do we add for the, um, what do we add for the click handler? And this is where functions come in. So you can define your own functions, which I'll show you how to do that in a second. You, you can define your own functions.

**Aaron Francis:** Fusion also provides some functions. In this case, we're gonna use sync. And what sync does is it takes all of the state that is not read-only, and it sends it to the back end. It does its thing, and then it sends it back to the front end. And so in this case, we're gonna type search on the front end. It's gonna change it. We're gonna send it to the back end, accomplish our search, and get the podcasts back on the other side.

**Aaron Francis:** It's very simple to do this. The first thing I'm gonna do is I'm gonna mark this podcast prop as read-only, because I actually don't want it to be sent back to me. I don't care what the front end says the podcasts are. I will compute the podcast. So we're gonna mark that as read-only. And then down here on the button, instead of saying console.log, we're just gonna say Fusion Sync.

**Aaron Francis:** And this is that function that is provided to you. So if we change this to boot and we hit search, we get Bootstrapped web. And that's 'cause we're sending the search back, we're running this function again, and we're, um, assigning this value to the podcast prop and sending that back out to the front end.

**Aaron Francis:** So what else is there? We've got, uh, Mostly Technical, great podcast. Mostly Technical with Ian Landsman and Aaron... That's me. I'm the dot dot dot. So now we have our full round trip going on, and like I said, there's no automatic round tripping. You're still in control. You're still in charge of when and what you send to the back end.

**Aaron Francis:** I wanna show you one more nicety that these Fusion functions have built in on the front end, and then I'll show you how to define your own function very quickly. And then if you don't like this syntax, there's an entirely separate syntax that you can use, and I'll show you that as well. The nicety that I wanna show you is this. Right here, we're defining loading as a prop, but on the back end, I don't think that makes sense.

**Aaron Francis:** And so we gotta get out of this mindset that because we can define something on the back end, we must define something on the back end. No, we don't have to do that. In fact, in this case, we don't have to define loading at all. You'll look down here and you'll see, hey, we have a script tag. We could define our own data. You don't just have to use data that was provided by Fusion. You can define your own.

**Aaron Francis:** There's lots of things you can do. Um, but instead of having to handle this, uh, loading state, every Fusion function, and so that's every function that Fusion provides and every function that you write yourself, every Fusion function has a little bit of magic built in. And so we can say, um, if Fusion sync is in progress, and we come back over here.

**Aaron Francis:** Uh, it's actually gonna be too fast, so let's do, um, a sleep of one. And now if we come back over here, it's sleeping for one, and we do mostly and hit search, you'll see that search, um, that loading indicator is handled by the Fusion function under the hood. And so instead of having to write, um, a state machine or is loading or anything like that, we handle that for you.

**Aaron Francis:** Now, what else do we handle? Well, there, there are a couple of things. Um, where should we put it? We can put it, uh, let's put it right down here. So if we type something here, what do we see? That's a good spot for it. We can do this. We can do, um, let's do a div, and we'll say, um, we'll do v if=FusionSync.uh, succeeded.

**Aaron Francis:** And then we can put success. And so if we run, um, tech and search for that, then you'll see success pop in there. Now, that's pretty cool, right? That's pretty cool, but if you've built a real application, you know you want success to go away now. Like, I get it. It worked. Go away.

**Aaron Francis:** And so we also have, um, we have, uh, succeeded, we have failed, but we also have recently, uh, succeeded. And so if you do recently succeeded and you say tech, it says, "Great, that was a success. Now, get back to work." And it just goes away. So we have a lot of those niceties built in for you, um, because we, we know how to build real applications, and we know that this is the kinda stuff that you want, and you don't want to be writing every Fusion

**Aaron Francis:** function. Comes with that. Let me show you how to define your own Fusion function. Defining your own function is, is extremely easy. All you do is, uh, you just call this expose. You call this expose function here, and then you use, um, you use PHP's named parameters to pass in whatever you want. Named arguments, you can say, um, favorite is a function, or you could say favorite and unfavorite are both functions, or maybe we're just gonna call it toggle because we don't...

**Aaron Francis:** We're not worried about the global namespace. It's just this component. And toggle, eh, maybe makes sense in this context. We'll leave it as favorite, and if you wanted, you could just expose-- You can expose as many functions as you want. I don't care. Do whatever you want. Every time you expose a function, it's going to show up on the front end as a function. And so we can come down here, and when favorite is emitted, we can just say, actually, just call favorite.

**Aaron Francis:** That's gonna reach into the back end and do whatever you want. Now, when you're in the back end, you can treat this just as a normal API endpoint. You can just return some data, or you can return some data and say, "Hey, Fusion, sync up podcasts," or sync up this other thing. So there are a lot of options that you can do in here. I'm not gonna go over them all right now.

**Aaron Francis:** Um, you can also, if you don't wanna just call it in the template because maybe you are just treating it as an endpoint and you need to, like, handle the data, for goodness sakes, you can, um... You see I do have a script tag down here. You can import a function called useFusion, and you can get that favorite function out of it. You can get all the state out of it, but you can also get the favorite function out of it and then wrap that up and use that however you want down here in actual JavaScript.

**Aaron Francis:** Now, if you hate this syntax, if you're looking at this and you're like, "Man, that's not PHP. I hate it," that's fine. I'm okay with that. We have an alternative syntax for you. Um, there is... What, what, what are we on? I think this is hello. So there is an entirely different syntax that may make a little bit more sense, um, to...

**Aaron Francis:** It makes more sense to me 'cause I'm a PHP developer. So we can do return new class, and what do we have here? Name, we can do public, uh, string name=Aaron. And so if we do that, then we get, uh, unexpected end of file. If we do that, then we have our state back. And so you've got a lot of things in here.

**Aaron Francis:** You've got, um, you've got public function mount. Um, and that's where you can receive your route model bindings. If you wanna do public function favorite, there is one, um, there's one safety thing that you have to do if you, uh, define a function like this and you want it to be available in the front end.

**Aaron Francis:** You have to tag it with the expose- um, attribute here because I don't want you, I don't want you writing, um, there are no bindings. That's fine. I don't want you writing, um, I don't want you accidentally writing public functions on the back end and then them getting sent to the front end. Any public properties you have, um, will be sent to the front end unless you tag it as, if you tag it as server only, that will not be sent to the front end.

**Aaron Francis:** But this is not, this is not a documentation video. This is a hype video, so I hope you're as hyped about this as I am. Let me tell you a little bit about where we're headed, um, and then how you can help. So right now, this only works with Vue. I like Vue. I think Vue's amazing. React is very, very big. So we're gonna make it work with React as well, and then after that, we'll look into Svelte. There are also a lot of, um, there are a lot of nice things we want to add.

**Aaron Francis:** Right now, we're defining, uh, state and functions at the page level, which is great, but you might have a, you might have a single function that is tied to a podcast, right? So if you have a podcast row, you might have a function where you can, you know, accomplish the favorite within there instead of at the page level. So we're gonna have, um, PHP functions for components instead of just pages.

**Aaron Francis:** We're gonna have, uh, that full end-to-end type safety like I talked about. I already have it working where you can Command + click on the variable in the template, and it pops you up to PHP. It's very cool. So we got a lot of work to do, and the way that you can help is, um, give me feedback. Tell me, tell me the stuff that works, the stuff that doesn't, uh, what you want to see. You can come to aaronfrancis.com/backstage if you wanna join the community that's helping shape this thing up and support my work.

**Aaron Francis:** That would be amazing. This is Fusion. I think this is going to be the best way to work with JavaScript in your Laravel application. So I hope that you check it out. I'll leave a link to the repo down below. Please let me know what you think. It is very early, and I need a lot of help. Thank you so much. See ya.

---

## Related on aaronfrancis.com

- [#33 - Aaron Francis on Fusion, TUIs and Content Creation](/podcast/33-aaron-francis-on-fusion-tuis-and-content-creation-087d6b18) *(Podcast · 2025-02-13 · shared: fusion, vue, react)*
- [Aaron Francis: Building and Teaching Cool Things](/podcast/aaron-francis-building-and-teaching-cool-things-39697cbb) *(Podcast · 2025-03-11 · shared: fusion, vue, react)*
- [Aaron Francis is putting PHP in Your JS Files](/podcast/aaron-francis-is-putting-php-in-your-js-files-6f3117a7) *(Podcast · 2025-03-05 · shared: php in javascript, vue, svelte)*
- [Fusion: Write PHP inside your Vue and React components](/podcast/fusion-write-php-inside-your-vue-and-react-components-cdf3a9e4) *(Podcast · 2025-02-14 · shared: fusion, vue, react)*
- [Why Most Developers Overcomplicate Everything w/ Aaron Francis](/podcast/why-most-developers-overcomplicate-everything-w-aaron-francis-855db0fe) *(Podcast · 2025-03-20 · shared: vue, react)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `fusion` ([F-G](/llms/keywords/f-g.txt)), `php in javascript` ([O-P](/llms/keywords/o-p.txt)), `laravel javascript` ([K-L](/llms/keywords/k-l.txt)), `route model binding` ([Q-R](/llms/keywords/q-r.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
