0

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.

L.H.
  • 27
  • 9
  • Note, So I have not actually tried a great deal to fix this issue, i asked a friend who agreed i need to handle the URL, but in his years of working with Symfony3 he has never had to, so does not know. -- I have looked through the documentation, but as i don't know what I'm looking for i cant find the answer – L.H. May 01 '18 at 04:32
  • So, check `$topic` and return 404 then? – zerkms May 01 '18 at 04:34
  • Possible duplicate of [symfony2 and throwing exception error](https://stackoverflow.com/questions/10625491/symfony2-and-throwing-exception-error) – DarkBee May 01 '18 at 04:37
  • i need to check $category and $topic on this page. ((and even with this as is the href with {{category.name}} throws an error because i have grabbed the Topic repo instead of Category, there for {{category.name}} does not exist. i will fix that later, for now its just /forum/#/... in the live data – L.H. May 01 '18 at 04:45
  • @DarkBee. the issue is not catching the error, but checking URL Variables so i can throw the error. – L.H. May 01 '18 at 04:47
  • @L.H. how is it an "issue"? You need to check them - so check them. – zerkms May 01 '18 at 04:56
  • @zerkms, Sorry if im unclear, i have 'issues' that dont relate to code... but i do not know how to take the info in the URL and then check the database to see if it exists. that is what i am asking. how to do that. – L.H. May 01 '18 at 05:01
  • Take `$topic` and check what's inside. You don't need to touch url at all. – zerkms May 01 '18 at 05:03
  • Perhaps i don't understand, but i know what is in `$topic` its list the the topic name, ID and child threads by ID. this dose not help me confirm if `{category}` or `{topic}` pages should exist. i can use that data on the page, but not confirm if the user should get a 404 or not, without knowing how to compare it with the URL? – L.H. May 01 '18 at 05:11
  • `$repository->findOneBy(['name' => $slug]);` this will return `false` or more likely `null` when a topic is not found. When topic is null throw that `HttpNotFoundException` to trigger the 404 inside symfony – DarkBee May 01 '18 at 05:15
  • thank you @DarkBee i assume i can do the exact same with `{category}` and in the future `{thread}`? and thank you zerkms a just needed it said in stupid for me to understand, if one of you would like to post the answer i can accept it. – L.H. May 01 '18 at 05:22

1 Answers1

0

In order to know whether an item is found at current URL you just can test if $topic is NULL

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @Route("/forum/{category}/{slug}", name="page_topic")
*/
public function showTopic($slug){
    $repository = $this->getDoctrine()->getRepository(Topic::class);
    $topic = $repository->findOneBy(['name' => $slug]);
    if ($topic === null) throw new NotFoundHttpException('Topic was not found'); // This should activate the 404-page
    return $this->render('forum/article.html.twig', ['topic' => $topic]);
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58