pouvez vous m'aider concernant cette erreur s'il vous plait, car je ne vois pas d'où vient le problème.
Merci d'avance pour votre temps.
Impossible to access an attribute ("title") on a null variable.
Voici mon code :
Le controller :
<?php
namespace App\Controller;
use App\Repository\ArticleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Article;
class BlogController extends AbstractController
{
// affichage de tous les articles
/**
* @Route("/blog", name="blog")
*/
public function index(ArticleRepository $repo )
{
$articles =$repo ->findAll();
return $this->render('blog/index.html.twig', [
'controller_name' => 'BlogController',
'articles' => $articles
]);
}
//affichage de la homepage
/**
*
* @Route("/",name="home")
*/
public function home ()
{
return $this -> render('blog/home.html.twig');
}
//affichage d'un seul article
/**
* @Route("blog/{id}", name="blog_show")
*/
public function show(ArticleRepository $repo,$id){
$article = $repo -> find($id);
return $this -> render ('blog/show.html.twig',[
'article'=> $article
]);
}
}
la vue :{% extends 'base.html.twig' %}
{% block body %}
<article>
<h2 >{{ article.title }} </h2>
<div class="metadata"> Ecrit le {{ article.createdAt | date('d/m/Y') }} à {{ article.createdAt | date ('H:i') }}</div>
<div content>
<img src='{{ article.image }}' alt="">
{{ article.content | raw }}
</div>
</article>
{% endblock %}
l'entity :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="string", length=255)
*/
private $image;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
}