تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Starting a Laravel Package0:00

At this point, we do, in fact, have a working product. Everything functions the way we expect. But it's all linked to our current application. So if I wanted to use this in a new project, I would have to just copy everything over. I don't want to do that. So in this video, let's start transferring everything over to a custom package for Laravel that we can install through Composer. So I'm going to go to just a directory where I store various projects. So anything open source is contained within here. I'm going to make a new directory. And why don't we call this Dolly or any kind of branding? I don't know. The first thing that comes to my mind is Dolly. But that's not very good, is it? Who cares? You know what? It doesn't matter. Let's just go with that for now. And we can always change it in the future. Now, to set up a package, we need a number of various files, like a license file is useful.

matter. Let's just go with that for now. And we can always change it in the future. Now, to set up a package, we need a number of various files, like a license file is useful. Of course, a composer.json file, a .gitignore file, maybe a phpunit.xml file, a source directory, all of these various things. Now, the truth is there are some packages that will just kind of scaffold or generate this stuff for you. Now, for me, I'm not creating these sorts of packages enough to really warrant having a built-in tool to help with that. I might create a handful a year. So to be frank, I just create the files manually. But yeah, if you have a scaffolder that you like, definitely use that. So with that in mind, let's create a license.txt. This is where we just have our generic terms. It's generally good practice, especially if larger companies will use your project. They will usually want something like that declared. Next, we'll have a readme file,

Initializing composer.json1:26

our generic terms. It's generally good practice, especially if larger companies will use your project. They will usually want something like that declared. Next, we'll have a README file, a phpunit.xml file. We'll definitely be writing tests for this package. And of course, a .gitignore file. Okay, let's also create a src directory, and then a tests directory as well. Next, well, let's see where we are. Okay, that looks good. Next, let's do composer init to set up our composer.json file. And let's go with that repo name. And we'll say RussianDollCaching for Laravel. Next, author is fine. Minimum stability. I'll do dev for now. Package type, leave it blank. License MIT. And I do not want to define my dependencies at the moment. Okay, so that's fine for now. If we list the files once again, and cat our composer.json file, yeah, looks good. We also might want to add keywords and things of that nature, but we'll

so that's fine for now. If we list the files once again, and cat our composer.json file, yeah, looks good. We also might want to add keywords and things of that nature, but we'll get to it. So let's open this up within my editor. Let's begin by editing my license file. And I'm just going to paste in a little snippet. This is what I use for all my repositories, basic MIT license. Next, let's edit the .gitignore file. And let's just make sure that we ignore the vendor directory. And I think that's fine for right now. Next, we'll edit the README. And I'll just say to do for the time being. And let's close that out. Okay, so let's take a look at our repo. The README, good. Our phpunit.xml file, I'm going to paste in a little snippet that I often use. There we go. Yeah, pretty basic stuff. You'll see this in a lot of repositories. So yeah, save it to a little custom snippet so that you can reuse it whenever you need to. And do note here

Configuring PSR-4 Autoloading3:03

use. There we go. Yeah, pretty basic stuff. You'll see this in a lot of repositories. So yeah, save it to a little custom snippet so that you can reuse it whenever you need to. And do note here that we are setting bootstrap to the vendor/autoload.php file accordingly. Alright, so what else our license is good, our composer.json is good. But also, why don't we set up PSR-4 for autoloading for the source directory. So let's do this. Let's say autoload, specifically PSR-4 autoloading. And now this will be our root namespace. So let's do this. Actually, you know what, I don't want to do myself. Let's do laracasts/dolly. I'll put it in that repo. Now that means our namespace will be Laracasts\Dolly. And our source directory for that will be the source folder. Okay, cool. So let's do a composer dump-autoload. And we're good. Next, let's think about what we will need for development. Well, I know we're going to have a ServiceProvider. So we need to pull in the Illuminate

Migrating Code Into Package3:58

let's do a composer dump-autoload. And we're good. Next, let's think about what we will need for development. Well, I know we're going to have a ServiceProvider. So we need to pull in the illuminate/support repo for this, like this. composer require illuminate/support, and pull that in. Okay, so if we come back, yeah, we have that. What else? Let's make sure this is jeffrey@lericast.com. And then finally, like I said, we might want to add some keywords. Laravel, cache, and let's just keep it like that for now. So let's begin moving files over. So let's just use the FileSystem for that. I'm going to pull up our dolly folder. And then in my desktop, let's go back to our RussianDollCaching lesson from before and open that as well. Okay, so we'll just start moving stuff over. For example, in our app directory, let's grab that and bring it over our Cacheable trait. Next within HTTP Middleware, we had that FlushViewCacheMiddleware

so we'll just start moving stuff over. For example, in our app directory, let's grab that bring it over our cacheable trait. Next within HTTP middleware, we had that flushViewCache middleware that we might use. And also, I know we're going to have to modify this. But for the time being, let's at least grab our AppServiceProvider. And that's where we registered our Blade directives. So we'll migrate that over to a custom ServiceProvider for our package. And yeah, I think that'll do it. So if we come back and refresh the folder, here we go. So now I can begin adjusting these namespaces. For example, this will now be lericast/dolly. Accordingly, let's go to cacheable, update that. And now for AppServiceProvider. Yeah, I know I'm going to have to update this to how about DollyServiceProvider. And in fact, let's go ahead and rename that right now.

Updating Namespaces and Provider5:35

I know I'm going to have to update this to how about DollyServiceProvider. And in fact, let's go ahead and rename that right now. DollyServiceProvider. And yeah, notice how we're pulling in Illuminate\Support. Well, if we want to have access to that, yeah, that's specifically why we required it right here, so that we have access to it. Next, let's see what else if we scroll down? Well, I happen to know I'm going to change this but for the time being, but let's just honor what we currently have. So what this will be lericast/dolly/russian-caching. But yeah, I know in the future, maybe in the next lesson, we're going to set up a singleton and adjust this a little bit. But just keep that in mind for now. Okay, let me look over this. This looks pretty good to me. We next have our middleware that we're going to update to lericast/dolly/middleware.

But just keep that in mind for now. Okay, let me look over this. This looks pretty good to me. We next have our middleware that we're going to update to laracasts/dolly/middleware. And in fact, I'm just going to leave that off entirely. So laracasts/dolly. And then why don't we change this to flashViews. And I'll rename that. There we go. So that looks good. Next, our ServiceProvider, we've already looked at that our Cacheable. Now this is the trait that you would include in your model. So you would just say include laracasts/dolly/Cacheable. And that will just add a single method here called getCacheKey. Now it would be cool if in the future, at some point, Laravel would offer something like this right out of the box. But right now it doesn't. So here's what we're going to do, I'm going to come back, let's go back to open_source/dolly, I will initialize get,

Git Commit and Next Steps7:06

this right out of the box. But right now it doesn't. So here's what we're going to do, I'm going to come back, let's go back to open-source/dolly, I will initialize git, and we'll say git commit with just our first initial commit. And we'll call it a lesson. So in the next video, I'm going to start writing some tests to make sure that everything works the way I expect. And I think what we'll find here is the test might drive us to a slightly different design. So I'll see you then.

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