Laravel route pass variable to controller

Laravel route pass variable to controller

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Passing Static Arguments to Controllers in Laravel

You are attempting to pass a hardcoded integer, such as 1 or 2, directly into a controller method definition within your route configuration. Specifically, you want to instantiate ProductsController@index(1) so that the $type_id parameter is set automatically without modifying the controller logic itself.

In standard PHP and Laravel syntax, this approach is not supported. You cannot append arguments inside the parentheses of a class method reference like ClassName@method(arg). The router expects either an unbound method (to be resolved later via dependency injection) or a Closure that can accept parameters from the request. When you write 'uses' => 'ProductsController@index(1)', Laravel interprets the (1) as part of the string rather than as a function argument, resulting in a syntax error or a failure to resolve the controller action.

To achieve your goal of hardcoding a type_id for specific routes like /milk and /cheese, you must explicitly bind that value when defining the route. There are three primary patterns to handle this scenario, ranging from passing arguments via a Closure to using route model binding nuances.

Using a Closure to Bind Arguments

The most robust way to pass static values to a controller method is by wrapping the controller reference in a Closure. This allows you to define the default argument for the $type_id parameter before the request hits the controller.

Route::get('/milk', function() {
    return ProductsController::index(1);
});

In this example, ProductsController::index(1) is executed immediately when the route is defined during the bootstrapping phase. The result (the view or response) is then cached or handled by the router depending on the specific Laravel version and context, but typically you want to pass the argument dynamically per request.

If you intend for the route to remain dynamic (i.e., different users might hit it at different times) but always default to type_id = 1, you should use a Closure that accepts the route's URI parameters and passes your hardcoded value alongside them:

Route::get('/milk', function($type_id = 1) {
    return ProductsController::index($type_id);
})->name('milk');

However, if your controller method signature is strictly defined as public function index($type_id), you cannot use route parameters (like $id or $slug) to fill that slot unless the parameter name matches exactly and no default value exists in the closure. Since you want a hardcoded static value, the cleanest approach is often to accept the controller without arguments and pass the value inside the Closure wrapper if strict typing isn't enforced, or simply inject the value into the view from the route definition.

Modifying the Controller Method Signature

If you strictly require the type_id to be an argument in the controller method, the most explicit solution is to define a default value within the method signature itself for those specific hardcoded routes.

class ProductsController extends BaseController {
    public function index($type_id = 0) {
        $Products = new Products;
        
        // Only query if type_id was passed or explicitly set, 
        // otherwise handle the fallback logic here.
        if ($type_id > 0) {
            $products = $Products->where('type_id', $type_id)->get();
        } else {
            // Handle default behavior for root index
            $products = new Products; 
        }

        return View::make('products.products', array('products' => $products));
    }
}

With this change, your routes can remain clean without passing arguments:

Route::get('/milk', ['as' => 'milk', 'uses' => 'ProductsController@index']);
Route::get('/cheese', ['as' => 'cheese', 'uses' => 'ProductsController@index']);

Inside the controller, you would need a way to distinguish between the hardcoded routes and generic calls. You might use attributes or check specific conditions, but often it is better to pass the value via the Closure if you want to keep the route definitions clean without altering the core logic of index.

Using Route Attributes (Laravel 5.2+)

In newer versions of Laravel, you can define a default parameter for the controller action directly on the route using the defaults array or by passing an array to uses where the second element is an array of arguments.

Route::get('/milk', [
    'as' => 'milk',
    'uses' => 'ProductsController@index',
    'defaults' => ['type_id' => 1] // Assuming you can map this, or use closure wrapper
]);

Actually, the most direct equivalent to your desired syntax using defaults is limited because defaults usually fills missing parameters from the route URL, not hardcoded static integers for specific methods unless the method signature allows it.

The most reliable pattern that mimics index(1) without breaking Laravel's resolver is:

Route::get('/milk', function() {
    return (new ProductsController)->index(1);
})->name('milk');

This creates a new instance of the controller and calls the method with your hardcoded argument. It keeps the route definition clear, avoids modifying the global controller class signature for every single product type, and adheres to Laravel's routing conventions. This approach ensures that /milk always returns products of type_id 1, while /cheese can be similarly configured with its own closure wrapping index(2).

Our network