Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 2 years ago.
0

Yep, that is pretty straight forward(ish). You need to generate a unique 'slug' for each product. There are a few packages out there that can help with this. You can also use Laravels slug helper too.

After that, you can just use your slug instead of the ID.

Route::get('display_item/{slug}' ....)

Then in your controller, just query on the slug instead of the ID.

Does that make sense?

Last updated 2 years ago.
0

in routes.php you add all your route before example route

Route::get('{name}','ProductController@getProduct')->where('name','[A-Za-z-]+');
Last updated 2 years ago.
0

Thanks for your answer.

This solution means that i have to store every slugs of each products, and if it's possible, i would like to avoid it. I explain: If the name of the product change, the slug have to change, and so, people who have the url with the old slug can not access to the page.

Last updated 2 years ago.
0

Use the ID and slug in the URL, fetch the product by the ID.

Once you have the product, check the slug in the URL matches the slug of the product, if it doesn't 301 redirect to the new/correct slug.

Last updated 2 years ago.
0

I had the same issue in my project, and solution was quite easy. You just need to have a function to recreate slug when you save a model. For instance ;

class myModel extends Eloquent{
    ......
    public function create_slug(){
       if(isset($this->slug) && isset($this->title) && !empty($this->title)){
          $this->slug = Str::slug( $this->title );              
       }
    }
    public function save(){
       $this->create_slug();
        parent::save();
    }
    .......

}

so whenever you save the model it will recreate the slug with the title. If title is not changed your slug will be the same.

Last updated 2 years ago.
0

@soclosed:

The idea of a URL is to be a unique identifier. So if you don't want the URL to change on update of a title, then just don't update the slug on update. Only set it once on creation of a new product.

If you don't like having an old slug for a new title, I suggest you decouple the slugs from the products and put them in a separate table where they are then linked to the actual product_id that they represent. This means you can have more than one slug (old ones and the current one) for each product.

On update of a product title, you would then just create a new slug pointing to the product, but leave the old ones in the slugs table. ;-)

Last updated 2 years ago.
0

For absolute SEO goodness, you would add another column updated_slug_id on the slugs table pointing to the most recent slug for that product (you would have to update this column for all old slugs of the same product_id on each title update, but leave it NULL for the most current slug).

THEN, whenever you notice that the user requests an old slug, you can redirect the user with a 301 Permanently Moved HTTP code to the current slug. This prevents search engines from thinking you have doublicate content (because multiple URLs are pointing to the same product). ;-)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

soclosed soclosed Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.