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

Overview of Filament plugins0:00

Now we're going to be covering filament plugins. Specifically, we want to extract all of those custom components that we've built inside our app and put them in a plugin. And by doing so, we can then redistribute those custom components to other people using Packagist or any other Composer repository. A great guide to learn how to build Laravel packages is the Laravel documentation. This provides all the information you need to know about the different sorts of resources that you can register within Packagist, how they're structured, and how they interact with Laravel. Another great guide is Sparty's Laravel package training course, which is a video guide on how to build and release Laravel packages by the experts at Sparty. Another great resource you can look at is the filament plugin Skeleton. Now, this is a repository that you can find on GitHub, and it acts as a template for any plugins you might build in the future.

Another great resource you can look at is the filament plugin Skeleton. Now, this is a repository that you can find on GitHub, and it acts as a template for any plugins you might build in the future. I'm not going to be using it today, simply because I want you to understand exactly how a plugin is structured, and how easy it is to get started with a plugin. However, if you're looking to build more plugins after this, I would absolutely suggest that you look at the plugin Skeleton as an excellent place to start. Today, we're going to be using Sparty's Laravel package tools. And this is a pre-built service provider that you can use and configure using this PackageFluent object. And this makes building a service provider much easier. I'll come back to this in a bit.

Create plugin package directory1:21

And this makes building a service provider much easier. I'll come back to this in a bit. To start off, we actually want to create the directory where our plugin is going to live. So let's create a packages directory in our Laravel app to keep things nice and simple. So, packages. Inside this packages directory, we could have multiple packages. So let's give it a name. So I'm going to call mine filament-toolkit. Now, this filament-toolkit package is a composer package. So we want to initialize composer within that directory.

Initialize Composer package1:47

Now, this filament-toolkit package is a composer package. So we want to initialize composer within that directory. Let's cd into packages and into filament-toolkit. And we can run composer init, which will guide us through all the steps. The package name is going to be danharing-filament-toolkit. The minimum stability of this package should probably be dev. The package type can be a library, since we're going to redistribute it. The license is going to be MIT. And I'm not going to be defining any dependencies interactively. It's now going to ask us for PSR-4 autoload mapping.

And I'm not going to be defining any dependencies interactively. It's now going to ask us for PSR-4 autoload mapping. Now, autoloading is how composer maps a specific directory in your package or your app to a namespace and essentially register all of the PHP files inside of it. So yes, we do want autoload mapping enabled for our plugin. And I do want to confirm generation. Now it says that it's generated some files and it's set up autoloading. So let's see what it's generated. We have our vendor directory, a composer.json, and our source directory as well, which is where all of our PHP classes will be living.

We have our vendor directory, a composer.json, and our source directory as well, which is where all of our PHP classes will be living. To start with, we probably want to be gitignoreing this vendor directory so that it isn't redistributed. So new, gitignore, and add vendor to it. Now we've done that, we can look at our composer.json. So we have our package name here. We have our autoloading. I'm going to fix the capitalization there. And this is mapping this namespace to this source directory. Since filament plugins always require an installation of filament in the main level app,

Add dependencies and provider3:39

And this is mapping this namespace to this source directory. Since filament plugins always require an installation of filament in the main level app, we want to require that in our composer.json. So let's use filament/support. And the current version of filament is 3 at the time of recording. So let's use version 3. Now, as I mentioned earlier, we're going to be using these Sparty package tools. And this comes with a pre-built service provider. So let's make that service provider inside our source directory of the plugin. So new PHP class.

So let's make that service provider inside our source directory of the plugin. So new php class. I'm going to call it filament-toolkit-service-provider. The namespace is going to be dan-haring-filament-toolkit, like in our composer.json. And we're going to be extending Sparty's package service provider. Now, this has generated a class for us. And since this is an abstract class that has this configurePackage function, phpStorm's gone ahead and added this configurePackage method. If you're not using phpStorm, you might need to add this yourself.

phpStorm's gone ahead and added this configurePackage method. If you're not using phpStorm, you might need to add this yourself. You can use the PackageServiceProvider to look at the signature of this method and copy it over into your own ServiceProvider. Inside this configurePackage method, we can chain on methods onto the package object. Let's look at the example. So you can see we have this name method where we define the package name. And another thing we're going to want is the hasViews method as well. So let's define a name.

And another thing we're going to want is the hasViews method as well. So let's define a name. The name is going to be filament-toolkit. And the hasViews method is going to tell the package service provider to register any views that it finds within the resources/views directory. So let's create that directory, resources/views. And this is in the root directory of our package. Now, while this service provider will be auto-loaded as a PHP class by Composer.json, Laravel doesn't actually know that it should be registered into any Laravel app that this plugin gets installed into.

Register provider in Composer5:54

Laravel doesn't actually know that it should be registered into any Laravel app that this plugin gets installed into. So we need to let Laravel know that the package service provider exists. And we can do that with the extra key in our composer.json. Now, this extra key is going to have another key inside it called Laravel. And that is going to have another key inside of it called providers. And this is an array of service providers that Laravel needs to auto-load into your app when this plugin gets installed. Our service provider is in the danharan\filament-toolkit namespace. So let's use that.

Our service provider is in the danharan\filament-toolkit namespace. So let's use that. And the name of the provider is filament-toolkit. So let's copy that over as well. Now, when we install this, Laravel will register this provider. Finally, to actually get this to work within our Laravel app here, we need to require this plugin within our composer.json. Now, if you haven't done this before, it might seem a bit weird, but we basically want to tell Composer that since this plugin isn't actually added to Packagist yet,

Require local package in app7:00

but we basically want to tell Composer that since this plugin isn't actually added to Packagist yet, we should instead use the local version of it in our composer.json. So let's define what's called a repository. So repositories. And these repositories are going to be used by Composer to find any packages that it might need to install. Let's define a repository. And it's going to have a type of path because this is a local repository. We want to provide a URL, which is going to be the path to our package.

And it's going to have a type of path because this is a local repository. We want to provide a URL, which is going to be the path to our package. In this case, it's going to be packages/filament-toolkit. Now that Composer knows where to find our filament-toolkit package, we can require it. So let's go up and find danharan/filament-toolkit. Now, this doesn't actually have a version yet because we haven't tagged anything with git. So I'm just going to be using * as our version. Composer can now install any version of the filament-toolkit.

So I'm just going to be using star as our version. Composer can now install any version of the filament-toolkit, but in reality, it's only going to be installing the local version. We can check this works by using ComposerUpdate in our app. So ComposerUpdate. And in the logs of the ComposerUpdate, we should now be able to see that it's actually symlinked this packages filament-toolkit, and it's identified it as the correct package, so danharan/filament-toolkit.

and it's identified it as the correct package, so danharan/filament-toolkit. Now, any of the plugin's features will be available in your app. We can verify that this plugin has been installed successfully and that the service provider has been loaded by Laravel by having a look at this log here, which tells us that our package has a service provider that's been discovered.

The anatomy of a Laravel package. Setting up a <code>composer.json</code> file for a package. Using Composer to autoload classes from a package directory into a namespace. Creating a package service provider class using Spatie's Package Tools, allowing it to register package views. Registering a package service provider for autodiscovery by Laravel using Composer.

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