Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jandk4014's avatar

Stored Routes - Internal Linking

We're hosting a catalog site that has product descriptions for every item. Inside the product descriptions are words that can link to other products, each on their own page. What I want to do is create links from the product descriptions to other pages? What are some suggestions for adding route information inside of a static text database field?

Should I store the named route in the DB and deal with it on the blade side?

0 likes
4 replies
mushood's avatar

It depends on what you are exactly trying to do

For example, if its the same word in different texts, you can create a service called WordToLink. Each time you retrieve your text, you do something like this:

$productDescription = WordToLink::transformWordsToLinks($text);

// in the service, you do a str replace for the word to place the link

If you link are predefined in the text already, you can output them using {!! !!} to show the tags

jandk4014's avatar

That's a good idea. What would you recommend to use in order to look for words in their singular and plural form?

For example my thought with the service would be to loop over every word in description. If the word is found in a particular table(s) that are to be "linked", perform the logic to turn the word into a link. But I want to consider the plural.

Link for the words;

  • house <=> houses
  • fly <=> flies
  • baby <=> babies
  • church <=> churches
  • leaf <=> leaves etc...
mushood's avatar

Thank you @serhii75 did not know this helper. Isn't laravel just the best! Was making my own way too often.

@jandk4014 Be very careful with this. Look for exact match and take into consideration spaces when replacing text with links

For example, if you want to link the word "trouble" and your text has the word "troublesome"

Also be sure to take care of Letter casing.

Be wary of pasting url links. They could transform the links if it contains your linked words.

One thing that also bothered me would be the performance hit. Every request and you need to transform this text.

I thought about doing that client side, in JS, but also costly in terms of performance, just transferred the responsibility.

What I would suggest to do is to have in your DB, a transformed text for example description and description_transformed

When you create/edit the article, you run the transform text service ONLY ONCE! Transform your text into links. Keep the original text for when you edit, you dont need to transform back without tags, but on the front end, output the transformed text only.

Please or to participate in this conversation.