Symfony 1 - Routing - Creating a URL with dashes (manually)

A great way to display URLs is to using dashes.

Indeed, it is always better for an human being to see a reading URL instead of a succession of symbols.

That is what we will see in this Symfony 1 tutorial about routing.

Let's take an example.
We want to create a module that represents a tutorial for beginners.

Our application is named Badprog.
Our model might be called : BadprogBeginnerTutorial.
So the module might be called : beginnnertutorial.

We assume that we already created the model of this module.

It means that the BadprogBeginnerTutorial class already exist in lib/model/doctrine/.

We can now add it in the application

For the backend app:

$ ./symfony doctrine:generate-admin backend BadprogBeginnerTutorial --module=beginnertutorial

For the frontend app:

$ ./symfony doctrine:generate-module --with-show --non-verbose-templates frontend beginnertutorial BadprogBeginnerTutorial

Now that our module exist, refresh your application to see the module appears in your folders.

In the routing.yml, we have now to add the correct path that will be displayed in the URL.
Choose the backend or the frontend app, in this example I will use the backend side:

// apps/backend/config/routing.yml
badprog_beginner_tutorial:
  class: sfDoctrineRouteCollection
  options:
    model:                BadprogBeginnerTutorial
    module:               beginnertutorial
    prefix_path:          /beginner-tutorial
    column:               id
    with_wildcard_routes: true

It is important to note that the module must not have a dash in its name, because a new class will be added in modules/beginnertutorial/actions once the module will be created:

// apps/backend/modules/beginnertutorial/actions/actions.class.php
require_once dirname(__FILE__).'/../lib/commentexerciseGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/commentexerciseGeneratorHelper.class.php';

/**
 * beginnertutorial actions.
 *
 */
class beginnertutorialActions extends autoBeginnertutorialActions
{
}

If a dash had been added in the module name, the class would have been called:

class beginner-tutorialActions extends autoBeginnertutorialActions
{
}

And this way of writing class is just forbidden.

So you must write your module name without dash.
Only the URL can be.

To test it, open a browser and go to: http://nameproject/backend_dev.php/beginner-tutorial/

Great job! laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.