Symfony - Where to put my HTML code? (view)

Hi!

I’m very new into PHP frameworks and Symfony and I already started reading documentation. I’ll begin converting my old codebase after I grasp the basics, but there’s something I don’t understand, hopefully will with your help. I understand that code must be reusable in the PHP part, so please ignore my php code below, I am asking this question about the html (view, right?) part. I’m mainly a front end developer.

I don’t want to use Twig, so I added .php to my config.

Let’s say I have something like this. Where do I put this html code in my symfony app so I could get this page when I call /categories/categoryname ?

<div class="clearfix tag25">
    <div class="shiny-title left"><img class="img-shiny" alt=""/>Keywords
        <a id="addkeyword">Add</a>
    </div>
    <div class="tags">
        <?php
            foreach ($tag as $tag_name) {
                $tag_clean = Array();
                $tag_clean['tag_value'] = clean_url($tag_name['tag_value']);
                echo "<a class='taga' href='/book/{$get_id}/tag/{$tag_clean['tag_value']}'{$tag_name['tag_value']}</a>";
                }
        ?>
    </div>
</div>

you pass the render method the place (in symfony notation) where your template is. cf. http://symfony.com/doc/current/cookbook/templating/PHP.html

1 Like

Got it.

I’ve got another question to ask. I created two URL’s, one is to show books, the other one is for book comments.

<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller {
    /**
     * @Route("/book/{id}", name="book")
     */
    public function indexAction($id) {
        return $this->render('::default/book.html.php', array(
          'id' => $id
        ));
    }

    /**
     * @Route("/book/comments/{id}", name="bookcomments")
     */
    public function showComments($id) {
        return $this->render('::default/comment.html.php', array(
            'id' => $id
        ));
    }
}

While I use flat PHP, I was creating a different file for each job (book.php, comment.php, etc). I’m still reading the docs, but I wanted to ask this question. Do I need to keep this practice and create a new file for each job under /views or is it a bad practice? I know I need to keep my code clean.

This might give you an idea how to best structure your application.

Scott

I already read that page this morning but I’m kinda confused. Thanks.

I’m like:

I know how you feel. I am also still learning.

This is where I am at. LOL!

Scott

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.