@@ -25,7 +25,26 @@ After that, publish the configuration file by call the console command:
2525php artisan vendor:publish --tag=feeds
2626```
2727
28- ## Basic Usage
28+ ## Usage
29+
30+ ### Create Feeds and Feed Items
31+
32+ #### Use a Laravel Idea plugin for PHP Storm
33+
34+ You can also easily create the desired classes using the [ Laravel Idea] ( http://laravel-idea.com ) plugin
35+ for [ PhpStorm] ( https://www.jetbrains.com/phpstorm/ ) :
36+
37+ ![ ] ( .github/images/idea.png )
38+
39+ #### Use Artisan
40+
41+ ``` bash
42+ # This will create a `app/Feeds/UserFeed.php` file
43+ php artisan make:feed User
44+
45+ # This will create a `app/Feeds/Items/UserFeedItem.php` file
46+ php artisan make:feed-item User
47+ ```
2948
3049### Generate Feeds
3150
@@ -36,9 +55,38 @@ console command:
3655php artisan feed:generate
3756```
3857
39- ### Feed
58+ Each feed can be created in a certain folder of a certain storage.
59+
60+ To indicate the storage, reduce the property of ` $storage ` in the feed class:
61+
62+ ``` php
63+ class UserFeed extends Feed
64+ {
65+ protected string $storage = 'public';
66+ }
67+ ```
68+
69+ By default, ` public ` .
70+
71+ The path to the file inside the storage is indicated in the ` filiname ` method:
72+
73+ ``` php
74+ class UserFeed extends Feed
75+ {
76+ public function filename(): string
77+ {
78+ return 'your/path/may/be/here.xml';
79+ }
80+ }
81+ ```
82+
83+ By default, the class name in ` kebab-case ` is used. For example, ` user-feed.xml ` for ` UserFeed ` class.
84+
85+ ### Filling
4086
41- Create a feed class. For example:
87+ #### Feed
88+
89+ For example, we use this content for the Feed class:
4290
4391``` php
4492namespace App\Feeds;
@@ -78,9 +126,9 @@ class UserFeed extends Feed
78126}
79127```
80128
81- ### Feed Item
129+ #### Feed Item
82130
83- Create a feed item class. For example :
131+ For example, we use this content for the Feed Item class :
84132
85133``` php
86134namespace App\Feeds\Items;
@@ -155,12 +203,100 @@ According to this example, the XML file with the following contents will be gene
155203</users >
156204```
157205
158- ### Laravel Idea Support
206+ ## Objects, attributes and more
159207
160- You can also easily create the desired classes using the [ Laravel Idea] ( http://laravel-idea.com ) plugin
161- for [ PhpStorm] ( https://www.jetbrains.com/phpstorm/ ) :
208+ ### Setting the name of the root element
162209
163- ![ ] ( .github/images/idea.png )
210+ ``` php
211+ class UserFeed extends Feed
212+ {
213+ public function rootItem(): ?string
214+ {
215+ return 'users';
216+ }
217+ }
218+ ```
219+
220+ ### Adding attributes for the main section
221+
222+ ``` php
223+ class UserFeedItem extends FeedItem
224+ {
225+ public function attributes(): array
226+ {
227+ return [
228+ 'id' => $this->model->id,
229+ 'created_at' => $this->model->created_at->format('Y-m-d'),
230+ ];
231+ }
232+
233+ public function toArray(): array
234+ {
235+ // ...
236+ }
237+ }
238+ ```
239+
240+ ### Adding attributes for nested elements
241+
242+ > [ !NOTE]
243+ >
244+ > Reserved names are:
245+ >
246+ > - ` @attributes `
247+ > - ` @cdata `
248+ > - ` @mixed `
249+
250+ ``` php
251+ class UserFeedItem extends FeedItem
252+ {
253+ public function toArray(): array
254+ {
255+ return [
256+ 'name' => $this->model->name,
257+ 'email' => $this->model->email,
258+
259+ 'header' => [
260+ '@cdata' => '<h1 >' . $this->model->name . '</h1 >',
261+ ],
262+
263+ 'names' => [
264+ 'Good guy' => [
265+ '@attributes' => [
266+ 'my-key-1' => 'my value 1',
267+ 'my-key-2' => 'my value 2',
268+ ],
269+
270+ 'name' => 'Luke Skywalker',
271+ 'weapon' => 'Lightsaber',
272+ ],
273+
274+ 'Bad guy' => [
275+ 'name' => [
276+ '@cdata' => '<h1 >Sauron</h1 >',
277+ ],
278+
279+ 'weapon' => 'Evil Eye',
280+ ],
281+ ],
282+ ];
283+ }
284+ }
285+ ```
286+
287+ ### Header information
288+
289+ If it is necessary to change the file cap, override the ` header ` method in the feed class:
290+
291+ ``` php
292+ class UserFeed extends Feed
293+ {
294+ public function header(): string
295+ {
296+ return '<?xml version =" 1.0" encoding =" UTF-8" ?>';
297+ }
298+ }
299+ ```
164300
165301## License
166302
0 commit comments