Edit I use Symfony 3.2
Ok I've found the solution by my self. Here it is what I did.
Firstly read this from the symfony doc. Use ConvertParamater It is really easy to implement http://symfony.com/doc/current/best_practices/controllers.html#using-the-paramconverter
After you have implemented it:
Install StofDoctrineExtensionBundle
in composer.json
"require": {
"stof/doctrine-extensions-bundle": "~1.1"
}
Don't forget to update the appKernel.php file
<?php
// app/AppKernel.php
public function registerBundles()
{
return array(
// …
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
// …
);
}
Then I configure the config.yml file:
# app/config/config.yml
# Stof\DoctrineExtensionsBundle configuration
stof_doctrine_extensions:
orm:
default:
sluggable: true
Then I add a slug property to my News Entity:
private $slug;
Then I update my News.orm.yml file
slug:
type: string
length: 255
unique: false
gedmo:
slug:
separator: _
style: camel
fields:
- title
Then I do
doctrine:generate:entites myBundle:News
And also `doctrine:schema:update --force
This created a slug field in my News table. Because this field may not be null, I have manually entered some value so each row of my News table has a valid slug
Then in my NewsController I use this line of code:
$this->generateUrl('myroute_news_show_one_by_id', array('slug' => $news->getSlug()), UrlGeneratorInterface::ABSOLUTE_URL);
return $this->render('MyBundle:News:single_post.html.twig', [
'news' => $news,
'tags' => $arrTagNames
]);`
And in my Twig template I create link like to show one news:
<a href="{{ path('myroute_show_one_by_id', {'slug': news.slug}) }}">{{news.title}}</a>
I hope this could help someone else