Skip to content

Commit

Permalink
Add support to macro (#301)
Browse files Browse the repository at this point in the history
* Add support to macro

* Update README.md
  • Loading branch information
omarherri committed May 9, 2023
1 parent 8f05ef8 commit 40068ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,27 @@ class CommomController extends Controller
</html>
```

#### Using Macro
Using the same code in multiple places can be tedious, which is why this package includes a Macroable trait. This trait allows additional functionality to be added to a class that was not defined in the class definition, using a simple trait.

For example, imagine that you need to add meta titles and descriptions for your pages. You can add your Macroable functions in the AppServiceProvider or create a dedicated file for this purpose, and define your function as shown in the code snippet:
```php
SEOTools::macro('webPage', function (string $title, string $description) {
SEOMeta::setTitle($title);
SEOMeta::setDescription($description);
SEOMeta::setCanonical('http://current.url.com');
OpenGraph::setDescription($description);
OpenGraph::setTitle($title);
OpenGraph::setUrl('http://current.url.com');
OpenGraph::addProperty('type', 'webpage');
});
```

In your controller, you can use the following code to utilize the function:
```php
SEOTools::webPage('Page title', 'Page description');
```

#### API (SEOMeta)

```php
Expand Down
3 changes: 3 additions & 0 deletions src/SEOTools/SEOTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Artesaos\SEOTools;

use Artesaos\SEOTools\Contracts\SEOTools as SEOContract;
use Illuminate\Support\Traits\Macroable;

/**
* SEOTools provides implementation for `SEOTools` contract.
Expand All @@ -11,6 +12,8 @@
*/
class SEOTools implements SEOContract
{
use Macroable;

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 40068ef

Please sign in to comment.