I Symfony I try to open a page for example:
http://project:8888/products
On that page I like to show the headline with the slug:
<h1>{{page.slug}}</h1>
One problem is, that it takes really long to open the page. And the other problem is, that there is an error message, that there is no access to the attribute slug....
MyController.php:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\Pages;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Omines\DataTablesBundle\Controller\DataTablesTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
class DataTableController extends Controller
{
/**
* @Route("/{slug}", name="page")
*/
use DataTablesTrait;
public function showPage($slug)
{
$pages = $this->getDoctrine()->getRepository(Pages::class)->findAll();
$page = $this->getDoctrine()->getRepository(Pages::class)->find($slug);
$table = $this->getDoctrine()->getRepository(User::class)->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$json_string = $serializer->serialize($table, 'json');
$file = 'data/data.json';
file_put_contents($file, $json_string);
return $this->render('list.html.twig', ['pages' => $pages, 'page' => $page]);
}
}
list.html.twig:
{% extends 'base.html.twig' %}
{% block title %}Symfony{% endblock %}
{% block body %}
<div class="wrapper">
{{ include('inc/navbar.html.twig') }}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper" style="min-height: 866px;">
<section class="content-header">
<h1>{{page.slug}}</h1>
</section>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>E-Mail</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- /.content-wrapper -->
</div>
<!-- ./wrapper -->
{% endblock %}
{% block javascripts %}
{% endblock %}
pages entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PagesRepository")
*/
class Pages
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $template;
/**
* @ORM\Column(type="string", length=255)
*/
private $icon;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTemplate(): ?string
{
return $this->template;
}
public function setTemplate(string $template): self
{
$this->template = $template;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}
The error is:
Impossible to access an attribute ("slug") on a null variable.
page
? – ᴄʀᴏᴢᴇᴛ Jul 26 '18 at 11:59$page
is null. It seems it can't find a page with the slugproducts
in your database. – dbrumann Jul 26 '18 at 12:08performance
in your symfony profiler to try to figure out why. – v-gael Jul 26 '18 at 12:15