Commit b8bf435 0 parents commit b8bf435 Copy full SHA for b8bf435
File tree 4 files changed +144
-0
lines changed
4 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Install via composer
2
+
3
+ ` composer require mylesduncanking/laravel-auto-bind `
4
+
5
+ # Getting started
6
+
7
+ In your controller's construct method add the call to bind the properties.
8
+
9
+ ``` php
10
+ public function __construct()
11
+ {
12
+ AutoBind::bind($this);
13
+ }
14
+ ```
15
+
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;
21
+
22
+ class ClientsController extends \Illuminate\Routing\Controller
23
+ {
24
+ #[AutoBindAttr]
25
+ public Client $client;
26
+
27
+ ...
28
+ ```
29
+
30
+ Then just ensure that your route file contains the same name as your controller property.
31
+ ``` php
32
+ ...
33
+
34
+ Route::post('clients', [ClientsController::class, 'create']);
35
+ Route::get('clients/{client}', [ClientsController::class, 'read']);
36
+ Route::patch('clients/{client}', [ClientsController::class, 'update']);
37
+ Route::delete('clients/{client}', [ClientsController::class, 'delete']);
38
+
39
+ ...
40
+ ```
41
+
42
+ # Usage
43
+ You can then access the auto-bound properties via the class properties.
44
+ ``` php
45
+ public function read()
46
+ {
47
+ echo $this->client->id;
48
+ }
49
+ ```
50
+
51
+ Or add those bound properties directly into your view via the ` bound ` method.
52
+ ``` php
53
+ ...
54
+
55
+ use MylesDuncanKing\AutoBind;
56
+
57
+ ...
58
+
59
+ public function read()
60
+ {
61
+ $foo = 'bar';
62
+ return view('clients.read', array_merge(AutoBind::bound(), compact(['foo'])));
63
+ }
64
+ ```
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " mylesduncanking/laravel-auto-bind" ,
3
+ "description" : " Abililty to automatically bind properties in Laravel" ,
4
+ "keywords" : [
5
+ " laravel" ,
6
+ " route model binding"
7
+ ],
8
+ "homepage" : " https://github.com/mylesduncanking" ,
9
+ "license" : " MIT" ,
10
+ "authors" : [
11
+ {
12
+ "name" : " Myles Duncan-King" ,
13
+ "email" : " mdkwd@hotmail.com" ,
14
+ "role" : " Developer"
15
+ }
16
+ ],
17
+ "require" : {
18
+ "php" : " ^8.0|^8.1"
19
+ },
20
+ "autoload" : {
21
+ "psr-4" : {
22
+ "MylesDuncanKing\\ AutoBind\\ " : " src"
23
+ }
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MylesDuncanKing \AutoBind ;
4
+
5
+ #[\Attribute(\Attribute::TARGET_PROPERTY )]
6
+ class Attribute
7
+ {
8
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MylesDuncanKing \AutoBind ;
4
+
5
+ class AutoBind
6
+ {
7
+ public static array $ ignoreAutoBindProperties = ['middleware ' ];
8
+
9
+ public static function bind ($ class ): void
10
+ {
11
+ $ reflection = new \ReflectionClass ($ class );
12
+ $ properties = $ reflection ->getProperties (\ReflectionProperty::IS_PUBLIC );
13
+
14
+ foreach ($ properties as $ property ) {
15
+ $ name = $ property ->getName ();
16
+ $ type = $ property ->getType ()?->getName() ?? null ;
17
+
18
+ // Check if the property has the AutoBind attribute
19
+ $ shouldAutoBind = count ($ property ->getAttributes (Attribute::class)) > 0 ;
20
+
21
+ if (
22
+ /* No AutoBind attribute */ ! $ shouldAutoBind
23
+ /* No type hint defined */ || ! $ type
24
+ /* Is an ignored property */ || in_array ($ name , self ::$ ignoreAutoBindProperties )
25
+ /* Not bound in the route */ || request ()->route ($ name ) === null
26
+ /* Not a valid class */ || ! class_exists ($ type )
27
+ ) {
28
+ continue ;
29
+ }
30
+
31
+ $ class ->$ name = (new $ type ())->findOrFail (request ()->route ($ name ));
32
+ request ()->route ()->forgetParameter ($ name );
33
+ }
34
+ }
35
+
36
+ public static function bound ($ class ): array
37
+ {
38
+ $ boundParameters = [];
39
+ foreach (get_object_vars ($ class ) as $ key => $ value ) {
40
+ if (in_array ($ key , self ::$ ignoreAutoBindProperties ) || isset ($ data [$ key ])) {
41
+ continue ;
42
+ }
43
+ $ boundParameters [$ key ] = $ value ;
44
+ }
45
+ return $ boundParameters ;
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments