3

I'm just starting with Symfony 2.7.3.

I've been reading "the Book", trying to piece the new-to-me concepts together.

I've put together my 1st simple example -- my 'Front Page'.

With the code below, I' expecting to see a blank page with just the words "TEST TEST" on it when I nav to

http://example.com/app_dev.php/

Instead I see the standard Symfony Welcome Page,

Welcome to Symfony 2.7.3

Your application is ready to start working on it at: /srv/www/test1/symfony/
What's next?

Read Symfony documentation to learn
How to create your first page in Symfony

I can't figure out why I'm still seeing that and not my "TEST TEST".

I'm staring at this and know I have to have missed SOMETHING simple that should be obvious :-/ But what? Staring at it more isn't getting me anywhere.

Why don't I see the "TEST TEST", and what do I need to fix here?

./app/config/routing.yml
    _frontpage:
        path:      /
        defaults:  { _controller: AppBundle:FrontPage:index }


./src/AppBundle/Controller/FrontPageController.php
    <?php
    namespace AppBundle\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    class FrontPageController extends Controller
    {
        /**
         * @Route("/", name="index")
         */
        public function indexAction() {
            return $this->render('default/page-front.html.twig');
        }
    }

./app/Resources/views/base.html.twig
    <!DOCTYPE html>
    <html>
        <head>
            <title>{% block title %}this title{% endblock %}</title>
            {% block meta %}
                <meta charset="UTF-8" />
            {% endblock %}
            {% block stylesheets %}{% endblock %}
        </head>
        <body>
            {% block body %}{% endblock %}
            {% block javascripts %}{% endblock %}
        </body>
    </html>

./app/Resources/views/default/page-front.html.twig
    {% extends 'base.html.twig' %}
    {% block title %}Changed Title{% endblock %}
    {% block body %}
        <div><p>TEST TEST</p></div>
    {% endblock %}
    {% block stylesheets %}
    {% endblock %}
unknown
  • 45
  • 8

2 Answers2

2

Symfony has an AppBundle included in the default installation. This bundle contains a default controlled which contains the default action for '/'. Change this controller action to handle someother path and then your controller action corresponding to 'frontpage' will be executed.

In AppBundle\Controller\DefaultController, change the indexAction as gien below

class DefaultController extends Controller
{
    /**
     * @Route("/default", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', array(
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
        ));
    }
}

The same configuration was declared twice for the '/' path. The AppBundle was getting priority because of

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

line in your routing.yml file which is(most likely) declared before your route.

Praveesh
  • 1,257
  • 1
  • 10
  • 23
  • Changing "@Route("/",..." to "@Route("/default"," in AppBundle\Controller\DefaultController works. I see "TEST TEST" now. I'm not "telling" the DefaultController.php to be used in my router.yml. Is it really a Default then, that's hardwired into Symfony itself and will always be used? I was *going* to delete it because I wasn't planning to use it. Need to understand this before I do! – unknown Sep 01 '15 at 04:37
  • The same configuration was declared twice for the '/' path. The AppBundle was getting priority because of ````app: resource: "@AppBundle/Controller/" type: annotation```` line in your routing.yml file – Praveesh Sep 01 '15 at 04:42
0

Since you say tht you're starting with Symfony, the suggested path is to just delete the contents of the indexAction() method in the DefaultController class of the AppBundle and add your own logic there.

Nothing is "hardwired" in Symfony, but AppBundle + DefaultController + route named homepage are usually recommended to define the homepage of your application.

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44