Symfony 1 - Routing - Creating a basic route

In this tutorial of the Symfony 1 framework, we will see how to create a new easy basic route.

The url_for() method will help us to create this routing.
In the layout, let's first add a link to our new page, called BapdroG and its corresponding route, called routingBadprog.

// frontend/templates/layout.php
<li><a href="<?php echo url_for('routingBadprog') ?>">BadproG</a></li>

If you try to refresh your webpage, you will have a 500 error, saying that the route "routingBadprog" does not exist.

Let's then create it at the top of your routing.yml file.
For that we have to specify the URL (/badprog) and param (module and action name).
Replace modulename by the name of your own module.

// frontend/config/routing.yml
routingBadprog:
  url:   /badprog
  param: { module: modulename, action: badprog }

Refresh your webpage.
No error anymore?
Yes, if you try to click the BadproG link, a 404 error will be displayed, saying that the Action "matter/badprog" does not exist.

OK, let's create this action:

// frontend/modules/modulename/actions/action.class.php
public function executeBadprog(sfWebRequest $request)
    {
        $this->myvariable = 'Hello world, welcome on this Symfony tutorial :D';
        $this->myarray = array('one', 'two', 'three');
    }

Refresh your webpage.
A new 500 error occurs saying that the template "badprogSuccess.php" does not exist or is unreadable in "".

So, let's add it:

// frontend/modules/modulename/templates/badprogSuccess.php
<div><?php echo $myvariable ?></div>
<div><?php echo $myarray[0] ?></div>
<div><?php echo $myarray[1] ?></div>
<div><?php echo $myarray[2] ?></div>

All right, this time refreshing the webpage will result by this render:

// http://yourproject/frontend_dev.php/badprog
Hello world, welcome on this Symfony tutorial :D
one
two
three

Well done, you made it wink

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.