I'm trying to build a Forum that uses as few templates as it can to be truly dynamic using the database to populate the forum.
What I would like to do is have my controller check the Database and make sure that the URL exists.
The object is that only pages, that exists will exist. So someone typing into the address host.com/forum/foo/bar will get a error message '404 page does not exist' instead of a blank index template.
Im using Symfony 4, Docrine, Twig, Annotations & various other addons
Current Code
//src/Controller/Controller.php
/**
* @Route("/forum/{category}/{slug}", name="page_topic")
*/
public function showTopic($slug){
$repository = $this->getDoctrine()->getRepository(Topic::class);
$topic = $repository->findOneBy(['name' => $slug]);
return $this->render('forum/article.html.twig', ['topic' => $topic]);
}
This is the controller for a topics page, it currently loops all the threads in the topic. But as the {category} & {slug} are not checked before the page loads you could literally type anything and there will be no error, just a template with a blank section. (i did try {topic} instead of {slug} but as i cant work out how to handle the checking, it would give error)
//templates/forum/article.html.twig
{% extends 'forum/index.html.twig' %}
{% block forumcore %}
<div id="thread list">
<h4>{{ topic.name }}</h4>
<ul>
{% for thread in topic.childThreads %}
<li><a href="/forum/{{category.name}}/{{ topic.name }}/{{ thread.name }}"><h6>{{ thread.name }}</h6></a></li>
{% endfor %}
</ul>
</div>
{% endblock %}
As you can see from the twig template the links rely on the entity's $name field to generate the URL to every page, and is fully dynamic.
Thanks in advance, if you need anymore information pop in a comment and i can update this post.