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

Create Laravel Project0:00

Open your terminal and we're gonna create our first mobile application with native PHP. So I'm just gonna start with Laravel New, as I always do for my new Laravel projects. So Laravel new, we'll call this LARACASTS Mobile. Wouldn't that be great actually, if there was a Laracasts mobile app? Hmm. I like using Live Wire, so I'm gonna use that. We can skip the rest of these things. Cool. Uh, you can use whatever front end you prefer,

We can skip the rest of these things. Cool. Uh, you can use whatever front end you prefer, so don't feel like you've gotta use Live Wire, just because I'm using Live Wire, I just find it the most convenient to work with. So let's go into our project and the next thing that we're gonna do is install native PHP for mobile. Before we do that, it's worth noting that I've already set up my license

Configure Composer Repository0:52

Before we do that, it's worth noting that I've already set up my license and username so that I can download native PHP for mobile. So this isn't gonna prompt me for those details. However, you can't just install native PHP for mobile from Composer. We have to do some configuration in our composer, JSON. So let's follow these instructions so we can install native PHP for mobile. I'm just gonna copy this

so we can install native PHP for mobile. I'm just gonna copy this and we're gonna open the project up in our sublime's. Still my favorite. So Composer json. I'd like to stick the repository section somewhere near the bottom. This'll do, let's just format that properly and we're ready. Now we can jump back to the Terminal and Run Composer require native PHP mobile, the packages installed,

and Run Composer require native PHP mobile, the packages installed, but we need to do some setup because the mobile application isn't just a package that can live inside of our Laravel application. We actually need to bring the Swift and Kotlin applications over into our environment so that we can move everything around and do it all right inside the root of your application.

Install NativePHP Scaffolding2:17

and do it all right inside the root of your application. And to get to that, we need to install native PHP, so we have a nice command to do that PHP Artisan Native install, and we're gonna install it for both platforms. In this case, I'm not gonna use the ICU. We'll talk about that in a bit, right? So it's installed. That was easy. Let's see what this has done. Let's jump back to Sublime.

So it's installed. That was easy. Let's see what this has done. Let's jump back to Sublime. Hopefully you'll have noticed that there's now this native PHP folder in the root of our project. Let's have a look at what's in here. So there's a few folders, Android, iOS, and resources. The Android folder is our Android application, and the iOS folder is our iOS application. And the resources folder is for stuff

and the iOS folder is our iOS application. And the resources folder is for stuff that doesn't really belong in your application, but that is needed for native PHP. You should dive into each one of these folders and have a look at the code that's in here. It's open for you to read and you can even adapt it, but it's really important to note that these folders shouldn't be part of your Git repository. You don't want to be storing all of these files

that these folders shouldn't be part of your Git repository. You don't want to be storing all of these files and things in your repository. They're ephemeral. And that just means that anytime we upgrade the package or you change your application, these files might need to be destroyed and reinstalled, and that's fine, but it means that you can't really make changes to these applications just yet. In the future, we'll have ways for you to

to these applications just yet. In the future, we'll have ways for you to create custom Swift and Kotlin modules and install your own packages and things if you know that that's what you need. Okay, so we've installed native PHP into our Laravel application. We wanna try and run it, but there are some other steps that we need to follow. So let's go back to the docs.

Set NativePHP Environment Variables4:26

but there are some other steps that we need to follow. So let's go back to the docs. We've installed the Composer package. We've actually already run the installer, but it does say we should have done this before. Actually. Doesn't matter too much the timing of this. We just need these in place before we run our application. The most important one of these for now is this one, the native PHP app id. But I'm gonna copy all of these

the native PHP app id. But I'm gonna copy all of these and we'll put them in our MV file. So let's open our MV file. I am just gonna stick them here because it's convenient. Now, the app ID needs to be unique. It needs to be unique per application. But across all of the applications that are on a particular app store, I think it's best if you can use a single app ID across both

that are on a particular app store, I think it's best if you can use a single app ID across both iOS and Android, it's gonna keep things much, much simpler. And to help with making sure that it's unique, the app stores encourage this sort of reverse DNS style naming where you put comm, which is short for company, and then your company. Now, that might be the business that you are working for. It might be the client whose project this is, or it could just be you as an individual.

It might be the client whose project this is, or it could just be you as an individual. In this case, I'm gonna choose laracasts as the company. And then the app name is laracasts Mobile. So I'm just gonna call it mobile com. RAAs mobile fine. That seems reasonable to me as being potentially unique. Now, we might find out later on that this actually is already taken, and that's okay. We can come back and change it if we need to,

Run App in iOS Simulator6:13

that this actually is already taken, and that's okay. We can come back and change it if we need to, but you shouldn't change your app ID too often. Once it's set and you know that it's unique and it's yours, you should hold onto it. But now I just want to run my application. So let's jump back to the terminal and run it PHP Artisan Native Run. And I'm gonna run this on an iOS simulator.

Artisan Native Run. And I'm gonna run this on an iOS simulator. We can just do a debug release for now. Now for iOS, it will show me a list of all of the devices that I've got connected and all of the simulators that are available within my Xcode installation. And sometimes there can be a lot of these. So finding the right one, you have to pay close attention. That's why we show you the device IDs here.

So finding the right one, you have to pay close attention. That's why we show you the device IDs here. You can use these in the command directly, so you don't have to go through this list all of the time, but we'll have to find one of those and pull it out. I want to use one of the latest devices with the latest version of iOS just to make sure that this app is working in that environment. So I'm gonna pick the iPhone 16 pro and I'm gonna run it on iOS 18.5.

So I'm gonna pick the iPhone 16 pro and I'm gonna run it on iOS 18.5. Now native PHP is configuring and installing, and then it's gonna compile the application and it's gonna install it onto that simulator, open the simulator and run the application for us. So we should see the brand new Laravel application home screen on a simulator. Once this is done. Now for the very first time that you compile your application, this will take some time.

Once this is done. Now for the very first time that you compile your application, this will take some time. It can take a few minutes, and it really depends on the spec of your Mac. And your Mac needs to be an M1 or above. And there it is. Our application is launched on the simulator. Isn't that great? It's only taken a few minutes to go from nothing to having Laravel running on a phone.

to go from nothing to having Laravel running on a phone. Brilliant. In the next lesson, let's have a look at some of the native features that we can tap into, and we'll have a look at an application that's already been built to use all of them.

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