I use include() to reuse code. How to reuse code in nette?
- netteman
- Member | 129
Hi
I often use include() to use the same classes, functions or other PHP code without copying and pasting the same code over and over again.
I guess nette has its own way of using the same code in different parts of a website.
So I'd like to ask what should I look for in the documentation if I want to get a piece of information from the database and print it out.
Thanks :)
- Šaman
- Member | 2667
Nette dont need include()
, it is RobotLoader and composer who
load all classes. Write your own class, put it in directory
where RobotLoader see it and you can use it everywhere.
- netteman
- Member | 129
Thanks for your help!
I got this code working (my custom class queries the database and presenter renders the title of the post with id 1)
Is it OK to give the custom class access to the database by $titleObject = new CustomClass($this->database); ?
Custom class
Presenter
Latte
Last edited by netteman (2016-05-08 17:20)
- CZechBoY
- Member | 3608
Register service CustomService
, inject model layer with
constructor or inject* method or @inject anotation.
https://doc.nette.org/…n/presenters#…
https://doc.nette.org/…dependencies
Last edited by CZechBoY (2016-05-08 18:33)
- netteman
- Member | 129
CZechBoY wrote:
Container creates the presenter so you dont create container in presenter.
Really? I thought the hierarchy is following: http://oi66.tinypic.com/w15pc8.jpg
- CZechBoY
- Member | 3608
Really, DIC is injected into presenter https://api.nette.org/…ter.php.html#1310
Last edited by CZechBoY (2016-05-11 19:46)
- netteman
- Member | 129
CZechBoY wrote:
btw why you need/want presenter as a factory for DIC?
I don't need it, I've never worked with a framework or dependency injection before so I'm trying to get a grasp of the concept of DI and how to use it properly.
greeny wrote:
He is not using Nette\DI\Container, but he created his own. @netteman you can benefit of Nette container instead of creating your own.
Thanks for your advice :) , I'll have a look at the documentation.