I faced similar kind of issue last time, I am still searching for some proper solution. https://www.onevanilla.one/
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?
@moamahfouz and - what is the problem to store value and extract it?
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?
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
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
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?
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.
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.
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>
It is my favorite. Not only for me but also for SEO. But I am asked to do it like that.
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
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.
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.
What you are probably looking to do is instead of using the auto increment column to use a key like /posts/this-is-my-post.
The first instance would be posts/{post} but we can improve this by change it to posts/{post:slug} This will then use the slug and not id field.
More information is here in the documentation https://laravel.com/docs/8.x/routing#customizing-the-default-key-name
Please or to participate in this conversation.