|
| 1 | +--- |
| 2 | +title: Model / Container Injection |
| 3 | +--- |
| 4 | +import FrameworkTabsCode from '../../../components/framework-tabs-code.astro' |
| 5 | +import FrameworkTabs from '../../../components/framework-tabs.astro' |
| 6 | +import {Aside} from "@astrojs/starlight/components"; |
| 7 | + |
| 8 | +Props and Actions can take can inject dependencies just like regular Laravel controllers. |
| 9 | + |
| 10 | +```php |
| 11 | +use Ozmos\Viper\Attrs\Prop; |
| 12 | +use Illuminate\Http\Request; |
| 13 | + |
| 14 | +return new class { |
| 15 | + #[Prop] |
| 16 | + public function user(Request $request) |
| 17 | + { |
| 18 | + return $request->user(); |
| 19 | + } |
| 20 | +}; |
| 21 | +``` |
| 22 | + |
| 23 | +## Model Injection |
| 24 | + |
| 25 | +Models can be injected from the current URL just like Laravel's route model binding feature, or by using our `#[Bind]` attribute. |
| 26 | + |
| 27 | +### Route model binding |
| 28 | + |
| 29 | +You can configure how we resolve route model bindings in your `AppServiceProvider`. |
| 30 | + |
| 31 | +```php |
| 32 | +// app/Providers/AppServiceProvider.php |
| 33 | + |
| 34 | +<?php |
| 35 | + |
| 36 | +namespace App\Providers; |
| 37 | + |
| 38 | +use Illuminate\Support\ServiceProvider; |
| 39 | +use Ozmos\Viper\Facades\Viper; |
| 40 | + |
| 41 | +class AppServiceProvider extends ServiceProvider |
| 42 | +{ |
| 43 | + public function boot(): void |
| 44 | + { |
| 45 | + // this is the default -> enabled and looks for models in the "\App\Models" namespace |
| 46 | + Viper::autoDiscoverModels(); |
| 47 | + |
| 48 | + // you can also configure a custom namespace to look in |
| 49 | + Viper::autoDiscoverModels("\\App\\V2\\Models"); |
| 50 | + |
| 51 | + // or disable model discovery completely |
| 52 | + Viper::autoDiscoverModels(false); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Then when you have a route parameter in your filename like `pages/posts/[post].php` if you typehint a model that matches the PascalCased version of the filename parameter we will inject it into your method. |
| 58 | + |
| 59 | +```php |
| 60 | +// resources/js/pages/posts/[post].php |
| 61 | +return new class { |
| 62 | + #[\Ozmos\Viper\Attrs\Prop] |
| 63 | + public function post(\App\Models\Post $post) |
| 64 | + { |
| 65 | + return $post; |
| 66 | + } |
| 67 | +}; |
| 68 | +``` |
| 69 | + |
| 70 | +### The `Bind` attribute |
| 71 | + |
| 72 | +<Aside type={"tip"}> |
| 73 | +This works for both Props and Actions |
| 74 | +</Aside> |
| 75 | + |
| 76 | +Sometimes you need to inject a model that isn't reflected in the url. |
| 77 | + |
| 78 | +Say you have an `/admin/users` route that lists all the users and you want to show the details in a modal when one of them is clicked on instead of redirecting to a page like `/admin/users/{user}`. In this case we can tell Viper that we want a model resolved manually by using the `#[Bind]` attribute. |
| 79 | + |
| 80 | + |
| 81 | +```php |
| 82 | +use Ozmos\Viper\Attrs\Prop; |
| 83 | +use Ozmos\Viper\Attrs\Bind; |
| 84 | +use Illuminate\Http\Request; |
| 85 | + |
| 86 | +return new class { |
| 87 | + #[Prop] |
| 88 | + public function user(#[Bind] User $user) |
| 89 | + { |
| 90 | + return $user; |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +Then in our frontend code we just need to pass the `bind` prop to our query. The key must be the same name as the php method parameter (typescript will also give you an error if you do not pass it correctly). |
| 96 | + |
| 97 | +<FrameworkTabs> |
| 98 | +<Fragment slot="vue"> |
| 99 | +```vue |
| 100 | +<script setup lang={'ts'}> |
| 101 | +import { usePage } from '@ozmos/viper-vue'; |
| 102 | +
|
| 103 | +const page = usePage<ViperGen.AdminUsers>(); |
| 104 | +
|
| 105 | +const selectedUserId = ref(1); |
| 106 | +
|
| 107 | +const { data: user } = page.useQuery('user', { |
| 108 | + bind: { |
| 109 | + user: selectedUserId, |
| 110 | + }, |
| 111 | +}); |
| 112 | +</script> |
| 113 | +``` |
| 114 | +</Fragment> |
| 115 | +<Fragment slot={'react'}> |
| 116 | +```tsx |
| 117 | +import { usePage } from '@ozmos/viper-react'; |
| 118 | +import { useState } from 'react'; |
| 119 | + |
| 120 | +export default function Page() { |
| 121 | + const page = usePage<ViperGen.AdminUsers>(); |
| 122 | + const [selectedUserId, setSelectedUserId] = useState(1); |
| 123 | + |
| 124 | + const { data: user } = page.useQuery('user', { |
| 125 | + bind: { |
| 126 | + user: selectedUserId, |
| 127 | + }, |
| 128 | + }); |
| 129 | +} |
| 130 | +``` |
| 131 | +</Fragment> |
| 132 | +</FrameworkTabs> |
| 133 | + |
| 134 | +You can bind multiple models by adding multiple parameters to your function with bind attributes. Ensure that the method parameter name matches that of the frontend bind object keys. |
| 135 | + |
| 136 | +You can customise the column that is used to resolve the model. By default this is going to check the `id` attribute on the model but you can specify any column in the attribute: |
| 137 | + |
| 138 | +```php |
| 139 | +use Ozmos\Viper\Attrs\Prop; |
| 140 | +use Ozmos\Viper\Attrs\Bind; |
| 141 | +use Illuminate\Http\Request; |
| 142 | + |
| 143 | +return new class { |
| 144 | + #[Prop] |
| 145 | + public function post(#[Bind(column: 'slug')] Post $post) |
| 146 | + { |
| 147 | + return $post; |
| 148 | + } |
| 149 | +}; |
| 150 | +``` |
0 commit comments