Answer the question
In order to leave comments, you need to log in
How to get a record from the database by its ID without using the id in the url, in yii?
There is a page where there is a list of specialists /specialists, it contains links to the pages of specialists /specialists/last_name-first_name.html
To create a URL for the specialist page, I do this:
Url::toRoute(['specialists/show', 'url' => Translit::t($specialist->first_name.'-'.$specialist->last_name)])
Answer the question
In order to leave comments, you need to log in
You want the url to not have an id, but you want to search by it - a vicious circle. Miracles don't happen.
The easiest way is to make another url column in the specialist table and write the url there and find it. So you solve 1 more problem that you don’t see, the uniqueness of this url, since even with a unique pair of first_name and last_name, after transliteration, a non-unique value may result.
You can write data to this column when creating a specialist. But there is another problem, this is the persistence of the url. If the user changes first_name or last_name, then whether to change this url? After all, the link mass that has accumulated will lead nowhere, and if you do not change this url, then the question arises of the appropriateness of the approach as a whole. Look at the greats of this world who have passed this way - social networks. They don't automate this process, they generate a unique url for the profile and then offer a random change. It's justified. It is right.
Alternatively, sometimes they do this:domain.com/id_firstname_lastname
while in the action you can extract the id and find the user, everything after id_ is purely decorative. But I am not a supporter of this method, since it’s not ice to give records in the url id. And then it is better to give a hash, and since a hash, then again an additional column is needed and we come to the first option.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question