Skip to content

Commit 7ea4175

Browse files
Readme updates
1 parent b8bf435 commit 7ea4175

File tree

2 files changed

+75
-34
lines changed

2 files changed

+75
-34
lines changed

README.md

+72-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
`composer require mylesduncanking/laravel-auto-bind`
44

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+
```
614

715
In your controller's construct method add the call to bind the properties.
816

@@ -13,52 +21,86 @@ public function __construct()
1321
}
1422
```
1523

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.
2125

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()
2329
{
24-
#[AutoBindAttr]
25-
public Client $client;
30+
echo $this->client->id;
31+
}
32+
```
2633

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+
}
2841
```
2942

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
3145
```php
32-
...
46+
<?php
3347

3448
Route::post('clients', [ClientsController::class, 'create']);
35-
Route::get('clients/{client}', [ClientsController::class, 'read']);
49+
Route::get('clients/{client}/{tab?}', [ClientsController::class, 'read']);
3650
Route::patch('clients/{client}', [ClientsController::class, 'update']);
3751
Route::delete('clients/{client}', [ClientsController::class, 'delete']);
38-
39-
...
4052
```
4153

42-
# Usage
43-
You can then access the auto-bound properties via the class properties.
54+
### Controller file: app/Http/Controllers/ClientsController.php
4455
```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
4665
{
47-
echo $this->client->id;
48-
}
49-
```
66+
#[AutoBind]
67+
public Client $client;
5068

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+
}
5473

55-
use MylesDuncanKing\AutoBind;
74+
public function create()
75+
{
76+
request()->validate(['name' => ['required', 'string', 'max:40']]);
5677

57-
...
78+
$client = new Client();
79+
$client->name = request('name');
80+
$client->save();
5881

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+
}
63105
}
64106
```

src/AutoBind.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ public static function bind($class): void
3333
}
3434
}
3535

36-
public static function bound($class): array
36+
public static function bound($class, $data = []): array
3737
{
38-
$boundParameters = [];
3938
foreach (get_object_vars($class) as $key => $value) {
4039
if (in_array($key, self::$ignoreAutoBindProperties) || isset($data[$key])) {
4140
continue;
4241
}
43-
$boundParameters[$key] = $value;
42+
$data[$key] = $value;
4443
}
45-
return $boundParameters;
44+
return $data;
4645
}
4746
}

0 commit comments

Comments
 (0)