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

moamahfouz's avatar

Show single item with id in session

All the courses I watched before showed item page details by the Item Id. But I am in a situation that I do not want to pass the id as a parameter in the URL. I thought I could make it using the session.

What is the best way to do so?

0 likes
14 replies
moamahfouz's avatar

The problem is how could I redirect the user from the product list for example to a single view? Based on which action if I am not going to pass it as a param should he show the product details?

Snapey's avatar

You are unlikely to do what you want because presumably a user has to select a post to view and this requires passing the id in the request

1 like
moamahfouz's avatar

I have implemented something like:

each product in the product list

<a href="#!" onclick="event.preventDefault();
    document.getElementById('product_id').val({{$row->product_id}});
    document.getElementById('show_product').submit();">
    {{$row->product_name}}
</a>
<form id="show_product" action="/product/show" method="get" class="d-none">
    @csrf
    <input type="hidden" id="product_id" name="product_id">
</form>

controller

 public function showProduct(Request $request)
    {
        $row = Product::find($request->product_id);
        return view('show', compact('row'));
    }

How good is that? @snapey @silencebringer

automica's avatar

@moamahfouz

what will happen in your user refreshes or bookmarks the product page?

in your case, you will not see a product, even though you're using a GET.

you appear to be trying to do something which doesn't follow convention so would be good to unstandard your reason.

what's the issue with displaying product id? if you dont want to expose the id you could use UUID or make a slug for your product and use that?

moamahfouz's avatar

You are absolutely right about refreshing the single product page. It is horrible to resubmit the form again. It also gives an error if trying to reach only the URL itself.

Actually, the client is someone who has more years of experience than me. He told me that it is necessary to show any item without showing its id in the URL.

I thought it is clear and common to do so. As I said he has more years of experience.

automica's avatar

I would suggest he has a word with whoever is doing his SEO. They will tell him he needs to have a unique identifier to allow the product to be shared, bookmarked and page ranked.

1 like
MichalOravec's avatar

What's wrong with this?

Route::get('product/{product}', 'ProductController@show')->name('product.show');
public function show(Product $product)
{
    return view('show', compact('product'));
}
<a href="{{ route('product.show', ['product' => $row]) }}">
    {{ $row->product_name }}
</a>
moamahfouz's avatar

It is my favorite. Not only for me but also for SEO. But I am asked to do it like that.

Snapey's avatar

You can obfuscate the ID by using uuid or hashids so that its not obviously a database primary key.

Your friend with so much experience should know that REST requires a resource ID

Passing it instead as a hidden field means its hidden to those that don't care and easily accessed and manipulated by those that are curious/malicious

1 like
martinbean's avatar

All the courses I watched before showed item page details by the Item Id. But I am in a situation that I do not want to pass the id as a parameter in the URL. I thought I could make it using the session.

@moamahfouz I think you need to understand the basics better. A URL identifies something. If you have different items, then each item should have a distinct URL.

Actually, the client is someone who has more years of experience than me. He told me that it is necessary to show any item without showing its id in the URL.

Then tell them they need to understand how URLs work. URL design is literally the basics of SEO, so I think your client is lying to you when they say they have “years of experience” but then telling you to do crap like this.

If you want search engines to index your content, then your content needs to have URLs. It’s that simple.

1 like
martinbean's avatar

All the courses I watched before showed item page details by the Item Id. But I am in a situation that I do not want to pass the id as a parameter in the URL. I thought I could make it using the session.

@moamahfouz I think you need to understand the basics better. A URL identifies something. If you have different items, then each item should have a distinct URL.

Actually, the client is someone who has more years of experience than me. He told me that it is necessary to show any item without showing its id in the URL.

Then tell them they need to understand how URLs work. URL design is literally the basics of SEO, so I think your client is lying to you when they say they have “years of experience” but then telling you to do crap like this.

If you want search engines to index your content, then your content needs to have URLs. It’s that simple.

Please or to participate in this conversation.