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

jordan-dev's avatar

Laravel not ordinary URL structure

I have the task to create not ordinary URL structure for Laravel ecommerce project. URL for product: /category-slug/product-slug Example: /fruits/orange But Url for sub-category have the same structure: /category-slug/sub-category-slug Example: /fruits/citrus

My solution is:


Route( '/{category}/{var}' , function(Category $category, $var) {
	if( $sub_category = SubCategory::where('slug', $var)->first()  ) {
		return view('sub-category')->with('sub_category', $sub_category );
	}
	if($product = Product::where('slug', $var)->first() ) {
		return view('product')->with('product', $product);
	}
	abort(404);
});

But I dont like that we do extra request every time when visit product page. Maybe someone has already solved this problem, please provide your solution. Thanks in advance.

0 likes
2 replies
newbie360's avatar

@jordan-dev

i think the logic on every request is not very good, instead

/fruits/product/{product-slug}

/fruits/sub/{sub-category-slug}

webrobert's avatar

Yeah, it’s not ordinary because it’s not wise.

Use more routes.

I’d do something like…

/{cat}/p/{product}

/{cat}/{sub}/{product}

Please or to participate in this conversation.