I use include() to reuse code. How to reuse code in nette?

Notice: This thread is very old.
netteman
Member | 125
+
0
-

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 | 2659
+
0
-

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 | 125
+
0
-

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

<?php

namespace App\Presenters;

class CustomClass
{
    private $database;

    public function __construct($database)
    {
        $this->database = $database;

    }//end mtd

    public function returnTitle()
    {
        $title = $this->database->table("posts")
                ->where("id = ?", 1);

        return $title;

    }//end mtd

}//end class

Presenter

<?php

namespace App\Presenters;

use Nette;

class TitlePresenter extends BasePresenter
{
    private $database;

    public function __construct(Nette\Database\Context $database)
    {
        $this->database = $database;
    }//end mtd

    public function renderDefault()
    {
        $titleObject = new CustomClass($this->database);
        $this->template->title = $titleObject->returnTitle();
    }//end mtd

}//end class

Latte

{block}
    {foreach $title as $row}
        {$row->title}
    {/foreach}
{/block}

Last edited by netteman (2016-05-08 17:20)

CZechBoY
Member | 3608
+
0
-

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 | 125
+
0
-

I didn't choose registering a service but does this use dependency injection?

TitleClass.php (service?)

<?php
//this class(service) queries the database
namespace App\Presenters;

use Nette;

class Title
{
    private $connection;

    function __construct(Nette\Database\Connection $connection)
    {
        $this->connection = $connection;
    }//end mtd


    function returnTitle(){

        return $this->connection->query("SELECT title FROM posts WHERE id = ?", 1);

    }//end mtd

}//end class

ContainerClass.php (Container?)

<?php
//this class(container) returns an object and takes care of the dependencies
namespace App\Presenters;

use Nette;

class Container
{
    function createConnection()
    {
        return new Nette\Database\Connection('mysql:host=127.0.0.1;dbname=quickstart', 'root', '');
    }//end mtd

    function createTitle()
    {
        return new Title($this->createConnection());
    }//end mtd

}//end class

PresenterTitle.php

<?php
//this class(presenter) creates an object(container) which creates another object(service) that can returnTitle to the presenter
namespace App\Presenters;

class TitlePresenter extends BasePresenter
{
    public function renderDefault()
    {
        $container = new Container;
        $title = $container->createTitle();
        $this->template->result = $title->returnTitle();
    }//end mtd

}//end class

The code is working. Did I get the terminology right?

Last edited by netteman (2016-05-10 20:34)

CZechBoY
Member | 3608
+
0
-

Container creates the presenter so you dont create container in presenter.

netteman
Member | 125
+
0
-

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
+
0
-

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 | 125
+
0
-

Ah, so the order of commands I write can be presenter → container → service but the inner workings of Nette are a bit different.

Thanks, man :)

CZechBoY
Member | 3608
+
0
-

btw why you need/want presenter as a factory for DIC?

greeny
Member | 405
+
0
-

He is not using Nette\DI\Container, but he created his own. @netteman you can benefit of Nette container instead of creating your own.

netteman
Member | 125
+
0
-

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.