Skip to content

Commit 7eb7fba

Browse files
zralydg
authored andcommitted
component dependecies: Added EN translation (#492)
1 parent af4d2ad commit 7eb7fba

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

en/components.texy

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,86 @@ This parameter will be automatically passed in every link as a `GET` parameter u
203203
.[caution]
204204
Never trust persistent parameters blindly because they can be faked easily (by overwriting the URL). Verify, for example, if the page number is within the correct interval.
205205

206+
Components with dependencies
207+
===========================
208+
209+
When our component needs some things to work, like `PollControl` needs `PollManager` to manage voting and save polls, it can be done by [injecting these dependencies | dependency-injection] into the constructor:
210+
211+
/--php
212+
class PollControl extends Control
213+
{
214+
/**
215+
* @var App\Model\PollManager
216+
*/
217+
private pollManager;
218+
219+
/**
220+
* @var int Id of a poll, for which the component is created
221+
*/
222+
private $pollId;
223+
224+
/**
225+
* @param $pollId
226+
* @param App\Model\PollManager $pollManager model providing the voting
227+
*/
228+
public function __construct($pollId, PollManager $pollManager)
229+
{
230+
$this->pollManager = $pollManager;
231+
$this->pollId = $pollId
232+
}
233+
234+
/**
235+
* @param $voteId Option Id we voted for
236+
*/
237+
public function handleVote($voteId)
238+
{
239+
$this->pollManager->vote($pollId, $voteId);
240+
//...
241+
}
242+
}
243+
\--
244+
245+
OK, but how the `PollManager` is put into the `PollControl`'s constructor? That's job for [DI | dependency-injection] container. It put it there automatically through an interface we provide it:
246+
247+
/--php
248+
interface IPollControlFactory
249+
{
250+
/**
251+
* @param $pollId
252+
* @return PollControl
253+
*/
254+
public function create($pollId);
255+
}
256+
\--
257+
258+
And this interface has to be registered in our container in [neon | configuring#toc-custom-services]:
259+
260+
261+
/--neon
262+
services:
263+
- IPollControlFactory
264+
\--
265+
266+
Finally, we will use this factory in our presenter:
267+
268+
/--php
269+
class PollPresenter extends \Nette\UI\Application\Presenter
270+
{
271+
/**
272+
* @var IPollControlFactory
273+
* @inject
274+
*/
275+
public $pollControlFactory;
276+
277+
protected function createComponentPollControl()
278+
{
279+
$pollId = 1; // we can pass our parameter
280+
return $this->pollControlFactory->create($pollId);
281+
}
282+
}
283+
\--
284+
285+
That's all. Nette internally implements this interface and injects it to our presenter, where we can use it. It also magically passes our parameter `$pollId` and instance of class `App\Model\PollManager` into our component.
206286

207287
Advanced use of components
208288
==========================

0 commit comments

Comments
 (0)