Bonjour cher tous,
Voila je rencontre un petit problème avec mon code depuis maintenant 2 mois. J'ai tout donné pour tenter de le rensoudre, mais mes efforts semblent ne pas suffir. Merci de m'aider.
<?php
namespace App\Blog;
use App\Blog\Actions\BlogAction;
use App\Framework\Module;
use App\Framework\Renderer\RendererInterface;
use Framework\Router;
class BlogModule extends Module
{
const DEFINITIONS = __DIR__ . '/config.php';
public function __construct(string $prefix, Router $router, RendererInterface $renderer)
{
$renderer->addPath('blog', __DIR__ . '/views');
$router->get($prefix, BlogAction::class, 'blog.index');
$router->get($prefix . '/{slug:[a-z\-0-9]+}', BlogAction::class, 'blog.show');
}
}
<?php
/**
* Created by IntelliJ IDEA.
* User: root
* Date: 25/04/18
* Time: 00:33
*/
namespace App\Framework\Renderer;
class PHPRenderer implements RendererInterface
{
const DEFAULT_NAMESPACE = '__MAIN';
private $paths = [];
/**
* Variable globalemenet accessible pour toutes les vues
*
* @var array
*/
private $globals = [];
public function __construct(?string $defaultPath = null)
{
if (!is_null($defaultPath)) {
$this->addPath($defaultPath);
}
}
/**
* Rajoute un chemin pour charger les vues
*
* @param string $namespace
* @param null|string $path
*/
public function addPath(string $namespace, ?string $path = null): void
{
if (is_null($path)) {
$this->paths[self::DEFAULT_NAMESPACE] = $namespace;
} else {
$this->paths[$namespace] = $path;
}
}
/**
* Rend une vue
* Le chemin peut être précisé avec les namespace rajoutés via addPath()
* $this->>render(@blog/view)
* $this->>render(view)
*
* @param string $view
* @param array $params
* @return string
*/
public function render(string $view, array $params = []): string
{
if ($this->hasNamespace($view)) {
$path = $this->replaceNamespace($view).'.php';
} else {
$path = $this->paths[self::DEFAULT_NAMESPACE] . DIRECTORY_SEPARATOR . $view . '.php';
}
ob_start();
$renderer = $this;
extract($this->globals);
extract($params);
require($path);
return ob_get_clean();
}
/**
* Permet de rajouter les variables global à toutes les vues
*
* @param string $key
* @param mixed $value
*/
public function addGlobal(string $key, $value): void
{
$this->globals[$key] = $value;
}
private function hasNamespace(string $view): bool
{
return $view[0] === '@';
}
private function getNamespace(string $view): string
{
return substr($view, 1, strpos($view, '/')-1);
}
private function replaceNamespace(string $view): string
{
$namespace = $this->getNamespace($view);
return str_replace('@' . $namespace, $this->paths[$namespace], $view);
}
}
Décrivez ici ce que vous cherchez à obtenir
Je voudrais pouvoir faire fonctionner mon code.
Décrivez ici vos erreurs ou ce que vous obtenez à la place de ce que vous attendez :(
Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "App\Blog\BlogModule" cannot be resolved: Parameter $renderer of construct() has no value defined or guessable Full definition: Object ( class = App\Blog\BlogModule lazy = false construct( $prefix = 'prefix' $router = get(blog.prefix) $renderer = #UNDEFINED# ) ) in /var/www/projetConference/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:18 Stack trace: #0 /var/www/projetConference/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(155): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\ObjectDefinition), 'Entry "App\Blog...') #1 /var/www/projetConference/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(71): DI\Definition\Resolver\ObjectCreator->createInstance(Object(DI\Definition\ObjectDefinition), Array) #2 /var/www/projetConference/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(64): DI\Definition\Resolver\ObjectCreator in /var/www/projetConference/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 18