2
2
3
3
` composer require mylesduncanking/laravel-auto-bind `
4
4
5
- # Getting started
5
+ # Usage
6
+
7
+ In each controller you want to auto-bind properties, add the ` #[AutoBind] ` attribute to each property.
8
+ ``` php
9
+ use MylesDuncanKing\AutoBind\Attribute as AutoBindAttr;
10
+
11
+ #[AutoBindAttr]
12
+ public Client $client;
13
+ ```
6
14
7
15
In your controller's construct method add the call to bind the properties.
8
16
@@ -13,52 +21,86 @@ public function __construct()
13
21
}
14
22
```
15
23
16
- In each controller you want to auto-bind properties, add the ` #[AutoBind] ` attribute to each property.
17
- ``` php
18
- ...
19
-
20
- use MylesDuncanKing\AutoBind\Attribute as AutoBindAttr;
24
+ Then just ensure that your route file contains the same name as your controller property as normal Laravel route-model binding requires.
21
25
22
- class ClientsController extends \Illuminate\Routing\Controller
26
+ You can then access the auto-bound properties via the class properties.
27
+ ``` php
28
+ public function read()
23
29
{
24
- #[AutoBindAttr]
25
- public Client $client;
30
+ echo $this->client->id;
31
+ }
32
+ ```
26
33
27
- ...
34
+ Or add those bound properties directly into your view via the ` bound ` method.
35
+ ``` php
36
+ public function read()
37
+ {
38
+ $foo = 'bar';
39
+ return view('clients.read', array_merge(AutoBind::bound(), compact(['foo'])));
40
+ }
28
41
```
29
42
30
- Then just ensure that your route file contains the same name as your controller property.
43
+ # Full example
44
+ ### Route file: app/routes/web.php
31
45
``` php
32
- ...
46
+ <?php
33
47
34
48
Route::post('clients', [ClientsController::class, 'create']);
35
- Route::get('clients/{client}', [ClientsController::class, 'read']);
49
+ Route::get('clients/{client}/{tab?} ', [ClientsController::class, 'read']);
36
50
Route::patch('clients/{client}', [ClientsController::class, 'update']);
37
51
Route::delete('clients/{client}', [ClientsController::class, 'delete']);
38
-
39
- ...
40
52
```
41
53
42
- # Usage
43
- You can then access the auto-bound properties via the class properties.
54
+ ### Controller file: app/Http/Controllers/ClientsController.php
44
55
``` php
45
- public function read()
56
+ <?php
57
+
58
+ namespace App\Http\Controllers;
59
+
60
+ use App\Models\Client;
61
+ use MylesDuncanKing\AutoBind\AutoBind;
62
+ use MylesDuncanKing\AutoBind\Attribute as AutoBind;
63
+
64
+ class ClientsController extends \Illuminate\Routing\Controller
46
65
{
47
- echo $this->client->id;
48
- }
49
- ```
66
+ #[AutoBind]
67
+ public Client $client;
50
68
51
- Or add those bound properties directly into your view via the ` bound ` method.
52
- ``` php
53
- ...
69
+ public function __construct()
70
+ {
71
+ AutoBind::bind($this);
72
+ }
54
73
55
- use MylesDuncanKing\AutoBind;
74
+ public function create()
75
+ {
76
+ request()->validate(['name' => ['required', 'string', 'max:40']]);
56
77
57
- ...
78
+ $client = new Client();
79
+ $client->name = request('name');
80
+ $client->save();
58
81
59
- public function read()
60
- {
61
- $foo = 'bar';
62
- return view('clients.read', array_merge(AutoBind::bound(), compact(['foo'])));
82
+ return redirect()->route('client.read', $this->client);
83
+ }
84
+
85
+ public function read($tab = 'index')
86
+ {
87
+ return view('clients.read', AutoBind::bound($this, compact(['tab'])));
88
+ }
89
+
90
+ public function update()
91
+ {
92
+ request()->validate(['name' => ['required', 'string', 'max:40']]);
93
+
94
+ $this->client->name = request('name');
95
+ $this->client->save();
96
+
97
+ return redirect()->back();
98
+ }
99
+
100
+ public function delete()
101
+ {
102
+ $this->client->delete();
103
+ return redirect()->route('clients');
104
+ }
63
105
}
64
106
```
0 commit comments