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

marosmjartan's avatar

Passing data to the controller directly from route file

Hello, I'm working on such a simple e-commerce now. It is a classic e-shop with few categories. The idea is that I want to have a subpage for each category, right at the root of the url.

like this:

Route::get('/category-one', ...);
Route::get('/category-two', ...);
Route::get('/category-three', ...);

I am currently solving this by having an invokable controller for every single category. Of course, I don't like this solution very much. Because their logic is exactly the same, with the difference that I get a different category from the database.

So I want to create a universal controller, such as CategoryProductController. This solution seems great, but I can't do just:

Route::get('/{category:slug}', ...)

Because I will overwrite every root route. And I also loose ability to separetelly named this routes (which I would like to keep )

So my idea now is to pass data to controller directly from route file. Let me explain:

// CategoryProductController

public function index ($category_id)
{
	$category = Category::findOrFail($category_id);

	return view('category_view', [
		'products' => $category->products()
	]);
}

But I don't wanna pass the "$category_id", to url. Because I don't want to dirty my url :D

I will like to pass it directly from the route file:

Route::get('/category-one', [CategoryProductController::class, 'index', 1])
->name('category-one');

Route::get('/category-two', [CategoryProductController::class, 'index', 2])
->name('category-two');

Route::get('/category-three', [CategoryProductController::class, 'index', 3])
->name('category-three');

Is something like this possible? I will also be happy for any advice that may even tell me that I approach it completely badly.

Thank you....

0 likes
3 replies
martinbean's avatar
Level 80

@marosmjartan Yes, you’re going about this the wrong way.

Place your category route last and then it won’t overwrite any other “root” routes.

// All of your other routes...

// Your category route, last
Route::get('{category:slug}', [CategoryController::class, 'show'])->name('category.show');
class CategoryController extends Controller
{
    public function show(Category $category)
    {
        return view('category.show', [
            'products' => $category->products(),
        ]);
    }
}

I don’t know how you’re planning to have routes with set names when they’re coming from the database, either… Just route properly using a generic route name and the category slug:

@foreach($categories as $category)
    <li>
        <a href="{{ route('category.show', compact('category')) }}">
            {{ $category->name }}
        </a>
    </li>
@endforeach
marosmjartan's avatar

Since I get no answer for about a hour I solved it just like you. Thank you anyway.. :)

marosmjartan's avatar

But still... If I want more than 1 controller to work like this. This would not be the solution...

So the only way to pass value to the controller is via url, yes?

Please or to participate in this conversation.