How to generate SEO-friendly URLs using kohana?


by emelie , in category: SEO , 2 months ago

How to generate SEO-friendly URLs using kohana?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer


by zion , a month ago


In Kohana, you can generate SEO-friendly URLs by using the URL::title method to create a URL-safe version of your string and then using the Route class to map it to a specific controller action.

Here is an example to show you how to create a SEO-friendly URL in Kohana:

  1. In your controller action, format the string that you want to use in your URL:$title = URL::title($article->title);
  2. Create a new Route to map the URL to your controller action:Route::set('article', 'articles/<title>', array('title' => '[a-zA-Z0-9_-]+')) ->defaults(array( 'controller' => 'article', 'action' => 'view', ));
  3. In your view, generate the URL using the URL::site method:$url = URL::site('article/' . $title);

This will create a URL like for an article with the title "My Article Title".

Note: The Route class uses Regular Expressions to match URLs, so you can customize the pattern to match your needs. In this example, the pattern [a-zA-Z0-9_-]+ matches any combination of letters, numbers, underscores, and hyphens.