R
R
Racter2019-01-25 15:00:53
Web development
Racter, 2019-01-25 15:00:53

How to keep links in an article on a Laravel site from getting 404 errors after changing the route or slug?

Greetings!
I'm building a Laravel site here, and the situation is this... There is a blog on the site, and articles in the blog. In the articles, in theory, there will be links to other articles on this site.
There are routes like

Route::get('/blog/{slug}', [
    'uses' => '[email protected]',
    'as' => 'blog.show'
]);

(i.e. nothing in excess of the standard functionality), I take the slug from the database, from the plate along with id, text, etc.)
Now ... if I write something like this in the text of the blog article
<a href="http://prophecy.local/blog/hallou" >мой анкор</a>

then everything will be OK, but only until either the route changes (for example, in the route I will replace /blog/{slug} with /blog777/{slug}') or the slug!
You can, of course, insert a construction like
<a href="{{route('blog.show', ['post' => 'hallou'])}}">О сайте</a>

but again, it will not save if the slug changes! And it is still not clear how to insert a construction of this type from a simple user.
In a CMS such as Joomla, this issue is somehow resolved. There, through the WYSIWYG editor, a link with the article id is inserted, which then automatically turns into a beautiful human-readable link. In this case, the link is updated with any change in the slug or NC formation template.
I would like it to be on my site too! How to do it?!
1. Probably, you need to somehow work first of all with the post id, and get the slug. I can even do this by writing a method in the post model and using it when working with the route
route('blog.show', ['slug' => \App\Post::slugFromId(12)])

is it the right way of thinking? With this design, changing the slug is really not a problem, and a possible slight change in the route will not create a problem either.
2. Maybe I could even stop at the above construction, but it is not clear how to give the user a convenient opportunity to insert such links through the visual editor of the article?!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question