How can I generate a friendly URL in Symfony php?

by genevieve_boehm , in category: SEO , a year ago

How can I generate a friendly URL in Symfony php?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

Member

by terrence , a year ago

@genevieve_boehm 

In Symfony, you can generate friendly URLs by using routing. To create a new route, you can define a new URL pattern in your application's routing configuration file. You can also specify any placeholders in the URL pattern and map them to variables in your controller method. To make the URL even friendlier, you can use the slug type for the placeholders.


For example, if you want to create a friendly URL for a blog post, you can define the following route:

1
2
3
4
# config/routes.yaml
post:
  path: /post/{slug}
  controller: AppControllerPostController::show


Then, in your PostController:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use SymfonyComponentRoutingAnnotationRoute;

class PostController
{
    /**
     * @Route("/post/{slug}", name="post_show")
     */
    public function show($slug)
    {
        // ...
    }
}


With this setup, when you navigate to /post/my-post-title, the show method of your PostController will be executed, and the value of $slug will be set to "my-post-title".

Member

by virginie , 5 months ago

@genevieve_boehm 

To generate the friendly URL, you can use the Symfony path function in your Twig templates or the generateUrl method in your controller. These functions will automatically generate the URL based on the route name and any required parameters.


In Twig:

1
<a href="{{ path('post_show', {'slug': 'my-post-title'}) }}">View Post</a>


In your controller:

1
2
3
4
5
6
7
8
9
// Symfony < 5.2
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;

public function show($slug, UrlGeneratorInterface $urlGenerator)
{
    $url = $urlGenerator->generate('post_show', ['slug' => $slug]);

    // ...
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Symfony 5.2 and later
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;

class PostController
{
    /**
     * @Route("/post/{slug}", name="post_show")
     */
    public function show($slug, UrlGeneratorInterface $urlGenerator)
    {
        $url = $urlGenerator->generate('post_show', ['slug' => $slug]);

        // ...
    }
}


In both cases, the generated URL will be /post/my-post-title.

by june.crooks , 5 months ago

@genevieve_boehm 

To make the generated URLs even more friendly, you can use the concept of slugs. A slug is a URL-friendly version of a string, typically used for titles or names. Symfony provides a slugify function that can generate slugs from strings.


To use the slugify function, make sure you have the "cocur/slugify" package installed. You can install it using Composer:

1
composer require cocur/slugify


Once installed, you can use the slugify function in your controller or Twig templates:


In your controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use CocurSlugifySlugify;

public function show($title, UrlGeneratorInterface $urlGenerator)
{
    $slugify = new Slugify();
    $slug = $slugify->slugify($title);

    $url = $urlGenerator->generate('post_show', ['slug' => $slug]);

    // ...
}


In Twig:

1
<a href="{{ path('post_show', {'slug': post.title|slugify}) }}">View Post</a>


With this, the generated URLs for your blog posts will be even more friendly and user-readable.