registrace vlastní služby v config.neon
- t_burger
- Člen | 8
Zdravim vsechny,
zacinam s nette a snazim se podle navodu vytvorit jednoduchy blog. Stahnul jsem si posledni veri nette 2.10.
Mam problem se zaregistrovanim sluzby. Hlasilo mi to spoustu ruznych hlasek, az jsem se dopracoval k teto:
Nette\DI\ServiceCreationException
No service of type App\PostsRepository found. Make sure the type hint in App\HomepagePresenter::inject() is written correctly and service of this type is registered.
Mozna mam chybu v pouziti jmennych prostoru, ale nepodarilo se mi to rozchodit. Diky predem za rady.
config.neon:
services:
- Model\UserManager
- App\RouterFactory
router: @App\RouterFactory::createRouter
posts: Model\PostsRepository
comments: Model\CommentsRepository
Repository.php (v adresari model):
<?php
namespace Model;
use Nette,
Nette\Utils\Strings;
/**
* Description of Repository
*
* @author
*/
abstract class Repository extends Nette\Object {
/** @var Nette\Database\Connection */
protected $connection;
public function __construct(Nette\Database\Connection $db) {
$this->connection = $db;
}
}
?>
PostsRepository.php (v adresari model):
<?php
namespace Model;
use Nette,
Nette\Utils\Strings;
/**
* Description of PostsRepository
*
* @author
*/
class PostsRepository extends Repository {
public function fetchAll() {
return $this->connection->table('posts')->order('date DESC');
}
}
?>
CommentsRepository.php (v adresari model):
<?php
namespace Model;
use Nette,
Nette\Utils\Strings;
/**
* Description of CommentsRepository
*
* @author
*/
class CommentsRepository extends Repository {
public function fetchArticleComments($post_id) {
return $this->connection->table('comments')->where('post_id = ?', $post_id);
}
}
?>
HomepagePresenter.php (v adresari model):
<?php
namespace App;
use Nette,
Model;
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
/** @var PostsRepository */
private $postsRepository;
/** @var CommentsRepository */
private $commentsRepository;
public function inject(PostsRepository $postsRepository, CommentsRepository $commentsRepository) {
$this->postsRepository = $postsRepository;
$this->commentsRepository = $commentsRepository;
}
public function renderDefault() {
$this->template->posts = $this->postsRepository->fetchAll();
}
}
?>
- Michal Vyšinský
- Člen | 608
Buď v inject metodě presenteru:
public function inject(Model\PostsRepository $postsRepository, Model\CommentsRepository $commentsRepository)
Nebo upravit use na:
use Nette,
Model\PostsRepository,
Model\CommentsRepository;
Osobně se přikláním ke druhé variantě.