در حال بارگذاری ...

مرور NativePHP برای دسکتاپ0:00

I promised that we would have a whole section about desktop, but honestly we ran out of time. So we've got one lesson and it's gonna be a whistle stop tool of building a desktop app with native PHP. Yep, you can do both. I still think that desktop apps are amazing. A desktop app offers something that you just can't get from the web and who knows how long it's gonna take

that you just can't get from the web and who knows how long it's gonna take before the web can do many of the things that you can do with a desktop application. But I do love building with web technologies. That's where I thrive, that's what I'm used to. I know those tools and I can build things really quickly with them. So being able to build desktop applications with native PHP on top of Laravel is a massive unlock.

Creating Laravel App0:46

So being able to build desktop applications with native PHP on top of Laravel is a massive unlock. So let's just see how easy it is to build a native PHP desktop app. Let's start a new Laravel application. Laravel new. We are gonna call it Laracasts Desktop. Nice and easy. I'm gonna skip through this so you don't have to wait. Okay. Our application has been set up and I'm ready to get going with building my project. So let's go into laracasts desktop folder.

Installing NativePHP Desktop1:16

and I'm ready to get going with building my project. So let's go into laracasts desktop folder. And now all we need to do is install native PHP. So it's as simple as composer require native PHP desktop. Once that's installed, we need to just initialize native PHP, set up the service provider and a few other bits and pieces. So PHP Artisan native install, just like we did on the mobile version.

So PHP Artisan native install, just like we did on the mobile version. I don't need the electron package. I do want the dependencies. And for this, I'm not gonna run this up. Cool. So we've got native PHP is now set up. We could go and look at what it's done. We could go and see what our project looks like and the changes that have been made. It's very minimal and we'll dive into exactly

and the changes that have been made. It's very minimal and we'll dive into exactly how it's working in a moment. But what I wanna do is just see my application running. So for that, I would normally do PHP Artisan native Work Run. Yeah, you were paying attention in the previous lessons. It's the same for desktop. So we can do native run And the process of booting up your application on desktop

So we can do native run And the process of booting up your application on desktop is way, way faster. In just a few seconds, our app is there. This isn't particularly pretty. As you can see, the window size is small. It's got the dev tools squashed in there and all of this. But let's just ramp that up. We've got a standard Laravel application. Fantastic. Now I'm gonna close this down

Running Dev Mode2:49

We've got a standard Laravel application. Fantastic. Now I'm gonna close this down because I actually want to run this in developer mode. And for that we've got a really cool command that we've added into your composer, JSON, as part of the install process. So you can run composer, native dev. This is gonna run a bunch of processes at the same time so that you can get going with development in whatever your favorite tool is,

that you can get going with development in whatever your favorite tool is, and you'll be able to see changes happening in your application in real time. It means that you don't have to keep quitting and rerunning your application to get things uh, tested. So I'm gonna open this up in Sublime and let's have a look at that actually working. Okay, so now I just wanna open this welcome blade file and make a change.

Okay, so now I just wanna open this welcome blade file and make a change. So let's find that deploy now button. We're just gonna change this to something simple and basic just to prove that this all works. So, hello World save. And there you go. It's refresh for us and our change can be seen straight away. Fantastic. So we're ready to just dive into developing our application.

How Electron Integration Works3:58

Fantastic. So we're ready to just dive into developing our application. NativePHP for desktop is built on top of electron and electron exposes a lot of the native APIs through a layer in node by default that is really only available to the electron environment. What we've done with native PHP is make a way for the Laravel application to speak to the node environment of electron and therefore expose many of the APIs that are available to

of electron and therefore expose many of the APIs that are available to Electron to our Laravel application. We don't have all of them yet, but we've got some of the fundamental ones that are really, really useful for building rich applications. So you can get building great application for desktop in no time at all. Let's see how this works a little bit under the hood so you can get an idea of what's going on.

Let's see how this works a little bit under the hood so you can get an idea of what's going on. Let's bring back the file structure again and we're gonna go into the vendor directory. I'm gonna go to native PHP. We can see our desktop package and before we look at the PHP code, we're gonna dive into the electron code and that's in resources electron and particularly our electron plugin specifically.

and that's in resources electron and particularly our electron plugin specifically. This is where we expose all of the things that native PHP can make use of. We've basically created a way to serve those over a kind of server it, it is technically a HTTP server that's running, but it's all secure. So don't worry about that detail. It's an implementation detail. It might go away in the future.

It's an implementation detail. It might go away in the future. For now, we have this very simple API and each uh, segment of the API is basically its own TypeScript file. Let's have a look at one of these. Let's take a look at the doc. So our doc API, which really only works on a Mac, the Doc API exposes ways to manipulate the doc icon of our application.

the Doc API exposes ways to manipulate the doc icon of our application. So that's cool. We can do all sorts of things. We can create sub menus, we can create, we can change the icon, we can change the badge count on the icon, and all of that's dynamic and live and and real time. So we can do some of those things in our application and it will just be reflected in the doc immediately. As you can see, these APIs are very straightforward.

and it will just be reflected in the doc immediately. As you can see, these APIs are very straightforward. They're simple functions that just call into the electron functionality. And once that's run, they return back to our Laravel application. We'll have a look at how Laravel calls these in a moment, but what I wanna highlight here is just how straightforward this is. If you can spend just a few minutes with a TypeScript file

how straightforward this is. If you can spend just a few minutes with a TypeScript file and working out where to put these things, you can create new functionality in native PHP either for yourself or contribute it back to the core of the project and have it available to everybody. We've had hundreds and hundreds of prs and contributions from the community and native PHP for desktop has only grown

and contributions from the community and native PHP for desktop has only grown because of the amazing support that we've gotten from the community. Now let's have a look at the PHP side outta the electron plugin. Out of our resources folder into the source of the package, you'll see a bunch of things. This is a mix of stuff. We've got tooling that enables your Laravel application

This is a mix of stuff. We've got tooling that enables your Laravel application to spin up the electron application and configure all of that stuff. But then when it's running inside the electron application, there are also a bunch of tools and classes here to make things work. It can be a little bit complicated to get your head around, but once you see the the basics of it, I think it'll be fine.

but once you see the the basics of it, I think it'll be fine. Let's have a look for this doc. Okay, I can see a class here, doc. Let's just see what that looks like. As you skim through some of these classes, you'll see they're also very straightforward. In most cases, they're just making this client post or get or something over to the electron side and it is just a HTTP call.

or get or something over to the electron side and it is just a HTTP call. It's all happening on your device, it's not going over the network. So it's very fast, it's very straightforward to build and you can see it just takes uh, the endpoint and the parameters and then whatever we do in that is kind of entirely up to the electron side. The important thing is that we've exposed it to PHP in a way that's just really developer friendly.

The important thing is that we've exposed it to PHP in a way that's just really developer friendly. So you don't have to leave Laravel to build any of this stuff. So let's say I just wanted to bounce the doc icon 'cause that's a neat feature if something's happened in your app in the background and you wanna bring the user's attention back to your app so that they can see what's been going on. So to do that, I can use this class directly,

that they can see what's been going on. So to do that, I can use this class directly, but I prefer to use facades As you'd expect. The facade is pretty straightforward. Knowing that it's there and that there's one for every one of these APIs means that it's really easy to get going with using them. If you've got IntelliSense or anything like that in your IDE with all of the doc hints

If you've got IntelliSense or anything like that in your IDE with all of the doc hints and things that we've got, you should be able to find what you need just by start typing code. The other thing about using facades means that we can test the Laravel side very, very easily. We've got assertions for many of these APIs that allows you to write tests against native PHP without having to actually spin up a native PHP application. This is really powerful stuff.

to actually spin up a native PHP application. This is really powerful stuff. Unfortunately we don't have time to go into all of that, but I'd encourage you to look deeply into how to do testing in native PHP. Well, having automated tests around the functionality of native PHP without having to boot up an application is a huge time saver. Okay, let's try and make our doc icon bounce. I'm gonna do this really simply just in our welcome blade.

Bouncing Dock Icon10:22

Okay, let's try and make our doc icon bounce. I'm gonna do this really simply just in our welcome blade. I'm not gonna go into a controller or create a live wire class or anything like that. We're just gonna do it here, which is a bit hacky, but it's quick and easy. So the first thing I need to do is import the facade. So I'm gonna at use native desktop facades Doc, that's that bit.

desktop facades Doc, that's that bit. And then I can just add the functionality right here at the top. For now, there's no reason to do it anywhere else. So let's just add PHP doc. We'll keep it simple. We're just gonna bounce and that's it. Now, I haven't saved this file. When I save the file, I expect the doc icon to bounce, so keep your eye on the bottom right corner of the screen

When I save the file, I expect the doc icon to bounce, so keep your eye on the bottom right corner of the screen and save. See, that happened immediately. This code ran, it called into the electron side, the express server that we've got running, and it ran the electron code to bounce the doc icon. So it did, it ran the native functionality under the hood. It's really simple, as you can see. It's very, very quick. And building on this is so much fun.

It's really simple, as you can see. It's very, very quick. And building on this is so much fun. You can have your applications up in no time. We've had hundreds and hundreds of apps built people coming to us and telling us how amazing it is to get a desktop app built Within just 20 or 30 minutes. You might ask, is this secure? Is it safe to build applications like this? Remember, when you ship software to your users, your code is on their machine.

Security and Open Source12:05

Remember, when you ship software to your users, your code is on their machine. It's outside of your security perimeter. So you need to be very careful about the kinds of things that you put in there. And you, you should assume that any of the code that you shared, no matter how secure you try to make it could be cracked open and seen by others. So you don't want like important business logic in there that you don't want other people to see.

So you don't want like important business logic in there that you don't want other people to see. If you've got anything like that, you should probably run that on your server that's inside your security perimeter and have your application just act like a client and speak to your API. And if you need to securely speak to your API, you should use secure storage to store things like API tokens so that you are not reliant upon shipping those tokens

to store things like API tokens so that you are not reliant upon shipping those tokens with your application. NativePHP for desktop is completely free. It's MIT licensed, so you can do whatever you want with it. It's fully open source. And we welcome contributions. So if you start using native PHP for desktop to build desktop applications, we'd love to have you come and contribute towards the project, but very open to having new contributors on board

and contribute towards the project, but very open to having new contributors on board and helping them get up to speed with how to build features into native PHP for desktop. Just come and join the Discord community. Reach out to one of the moderators, raise issues. Feel free to submit prs. And if you have any questions at all, come and ask me. Come and ask the team and we'll help you get building really cool native desktop applications.

Come and ask the team and we'll help you get building really cool native desktop applications.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟