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

When to Use Macros0:00

Previously, we discussed global configuration of filament components, and this is a feature that allows you to reduce repetition within your codebase by setting sensible defaults for each component class. However, within your codebase, you might have some repeated bits of configuration that aren't exactly appropriate to be extracted on a global level. For example, let's take this slug being generated from a name pattern we have here. It might be on the ProductResource, and the CategoryResource, and the BrandResource, and the PostResource, and the AuthorResource. And since this isn't really a global configuration for every single text input, we need a different sort of abstraction. This is where Laravel's macro feature comes in, and you can use this to dynamically add methods to any core class within Laravel, and indeed Filament. Let's create a macro for generating a slug from

Register Slug Macro0:46

macro feature comes in, and you can use this to dynamically add methods to any core class within Laravel, and indeed Filament. Let's create a macro for generating a slug from a name. So in our ProductResource, we can see this Live on Blur, and this After State Updated. Now in a ServiceProvider, we can register a macro. Let's go to the AppServiceProvider. Here we can use the class of the component we want to add the macro to. So in this case, we want to add a macro for the textInput. The name of the macro is going to be the name of the method that we want to dynamically add to the class. In this case, I want to add a generateSlug method to the class. The second argument to this macro method is a function that will be run when we call this generateSlug method. So let's add a function here, and inside this function, this is a reference to the textInput instance.

Apply and Test Macro1:28

is a function that will be run when we call this generateSlug method. So let's add a function here, and inside this function, this is a reference to the textInput instance that we're going to be modifying with our configuration. So in this case, we want to copy over Live on Blur and AfterStateUpdated from here and into our macro. Now we should return this from the macro function to allow the method to be chainable with others. Finally, back in the ProductResource, we should use our new generateSlug method. To check, we can visit the browser. Let's go to our ProductResource and create a new product. Now when I type in a new product name, and I blur the input, the slug has been generated just like before. If I visit the CategoriesResource, and I create a new category, I see the same pattern. So let's move this over. Copy the generateSlug, and visit the CategoryResource.

Make Class Macroable2:14

like before. If I visit the CategoriesResource, and I create a new Category, I see the same pattern. So let's move this over. Copy the GenerateSlug, and visit the CategoryResource. I see the Live on Blur and the AfterStateUpdated, so let's copy this. And let's now test this again. Our macro is now an abstraction on top of the FilamentCoreComponent class, and this macro can now be used on any text input, and it allows us to reduce repetition between multiple parts of our codebase without making configuration global. Now, since macros are a core feature of Laravel, we can also use them within our example TextInput class. So let's make that macro-able. If I close all of this, and find our TextInput, all you need to do to be able to make something macro-able is use the macroable trait from Illuminate\Support\Traits. Now when I go into the test form, and I make a new macro here, so TextInput, macro, the

Explore Macroable Internals3:19

need to do to be able to make something macro-able is use the macro-able trait from Illuminate\Support\Traits. Now when I go into the test form, and I make a new macro here, so TextInput, Macro, the name of the macro could be foo, just for testing. And I'm just going to dd something, like bar here. Could make use of the macro. You see the macro is working already, just by using that trait. Now, I think macros are a really cool feature of Laravel, with a really neat implementation. So I'm going to show you just that. How does the macro-able trait work? Let's go into it. As you can see at the top of the class, we've got a protected macros property. Now this property is static, because we need to be able to store macros on the class level, and not the object level. This macro method is being used to register a macro, and what it does is it puts the name of the macro as a key in the macros array,

on the class level, and not the object level. This macro method is being used to register a macro, and what it does is it puts the name of the macro as a key in the macros array, and the value as the function that should be run when the macro is called. Mixins allow you to macro all methods from one object into another. So for example, you might have five different macros for the same class, and mixins allow you to not have to call the macro method five times. How this works is it will get all methods that are public and protected, and it will register macros for them. This hasMacro method is being used to check if a macro is being registered for a specific name, and this flushMacros method is being used to clear all macros that were previously registered to that class. Now here's where the magic happens. We have two methods. One's called callStatic, and one's called call.

How __call Works4:58

used to clear all macros that were previously registered to that class. Now here's where the magic happens. We have two methods. One's called callStatic, and one's called call. So callStatic is being used by php to handle the case where a method is being called on a class and it doesn't exist. So for example, if I run textInput foo like that, since the foo method does not actually exist on the textInput object, php is going to call this callStatic method, and this is what Laravel uses to then run the macros. So first Laravel checks if a macro has been registered for that method, and if it's a closure, it's going to bind that closure to this class so that we can access things like this and static inside that macro function. And finally, it's going to call that macro function and pass in any parameters. This call function is very similar, but instead of it running macros

inside that macro function. And finally, it's going to call that macro function and pass in any parameters. This call function is very similar, but instead of it running macros for a particular class statically, it's running a macro on an object instance, and the function that is used for the macro is being bound to the actual instance of the object instead of the class itself.

The reasons why macros can be useful to extend a PHP class' functionality. Allowing a class to register macros with Laravel's <code>Macroable</code> trait. How to define a macro on a class. How Laravel is able to dynamically respond to a macro call on a class.

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