Slim PHP and GET Parameters

Slim PHP and GET Parameters

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

Stefan Bogdanescu

Founder & Senior Architect

Accessing GET Parameters in Slim PHP

Accessing query parameters in a Slim application is straightforward, but the underlying mechanism depends on where you are within the request lifecycle. Slim does not provide a global superglobal variable like $_GET to inspect all requests indiscriminately; instead, it relies on its own dependency injection container and middleware pipeline to make data available safely.

When handling a route that expects parameters in the query string, such as filtering datasets by location or radius, you typically access these values inside your route handler closure or within a named controller class. The most idiomatic approach involves injecting the Request object into your method arguments. Slim automatically resolves this dependency and passes the active request instance to your callback, provided you have defined the necessary dependencies in your application bootstrap file.

Consider a scenario where you are building an endpoint like /dataset/schools?zip=99999&radius=5. Your route definition might look something like this:

$app->get('/dataset/schools', function (Request $request) {
    $zip = $request->getParam('zip');
    $radius = $request->getParam('radius');
    
    // Logic to filter schools based on these parameters
});

In this example, $request->getParam() retrieves the value associated with a specific key. If the parameter is missing, this method returns null. This behavior is generally safer than checking for existence first, as it forces you to handle the null case explicitly in your logic. You can also use $request->getQueryParam(), which performs an additional check to ensure the parameter exists before returning the value, preventing silent failures if a developer accidentally accesses a missing key.

It is important to distinguish between different types of parameters in your URL structure. If you define your route with named segments like /dataset/{id}, those are passed into the container as a Route object or accessible via $request->getAttribute('route'). However, query strings appended to the end of any URL path are strictly handled by the query string parsing logic of the underlying PSR-7 implementation. This separation ensures that whether you define a route as /api/resource or /api/resource?filter=active, the framework treats the parameters consistently regardless of the path complexity.

One common pitfall occurs when dealing with boolean-like values in the query string. For instance, if a parameter is set to true or false in the URL, Slim preserves these as strings unless you explicitly cast them. If your API logic expects an integer for the radius, retrieving a string "5" will likely cause issues downstream. You should always validate and cast these values immediately after retrieval:

$radius = (int) $request->getParam('radius');
$zipCode = (string) $request->getParam('zip');

Failing to cast can lead to subtle bugs where arithmetic operations fail or where string comparison logic behaves unexpectedly. Always assume that query parameters are strings by default until proven otherwise.

If you prefer a more functional approach, you might want to extract all query parameters into an associative array at once using $request->getQueryParams(). This is useful when your handler function needs to pass the entire filter set to a service layer without manually unpacking every single key. However, this method returns an empty array if no parameters are present, which can sometimes be confusing if you expect specific keys to always exist.

For complex validation requirements, relying solely on Slim's built-in parameter retrieval might not suffice. In production environments where data integrity is paramount, consider integrating a dedicated validation library or middleware that normalizes these inputs before they reach your business logic layer. This keeps your route handlers clean and focused on orchestration rather than parsing.

Our network