Accessing Dynamic Route Parameters in Zend Framework 2 Controllers
Transitioning from Zend Framework 1 to ZF2 often reveals a fundamental shift in how routing and controller logic interact. In your specific setup, you have defined a segment-based route that captures a dynamic slug parameter using the regex [a-zA-Z][a-zA-Z0-9_\/-]*. The core question is whether this variable data can be accessed within the controller action that handles the request.
The answer is unequivocally yes, but the mechanism differs significantly from how you might expect based on legacy patterns or other frameworks. In ZF2, route parameters are not passed as query strings by default; they are injected directly into the controller method signature. This approach leverages the framework's dependency injection system to bind the data extracted during routing directly to your action methods.
To access the slug you defined in your route configuration, you simply declare it as a parameter in your controller action. When the router matches a URL like /my-article, it extracts my-article from the [:slug] placeholder and provides it to the index action of the BootController.
namespace Htmlsite\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class BootController extends AbstractActionController
{
public function indexAction($slug = 'home')
{
// $slug now contains the value from the URL (e.g., 'my-article')
// or the default value ('home') if no parameter was matched.
echo "Current slug: " . $slug;
// You can now use this to fetch data from your database
// $model = new Model();
// $content = $model->findBySlug($slug);
}
}
In this example, the controller method signature accepts $slug as an argument. If a request hits /home, the value of $slug will be home. If a request hits /tech-news, $slug becomes tech-news. The default value 'home' in your route configuration acts as a fallback for requests that might not match any specific segment or if the route definition implies a static landing page.
A critical detail to note is the order of parameters and the use of the default value. In ZF2, you can define defaults in the route options ('defaults' => ['slug' => 'home']), but relying solely on that inside the controller method requires careful handling. The framework passes the matched parameter first, then any defined defaults if the parameter wasn't explicitly matched. However, a cleaner pattern often seen involves letting the dependency injection container handle the resolution or accepting the value with a default directly in the method signature as shown above.
One common pitfall when migrating from ZF1 is expecting to access parameters via $_GET or $this->request()->getQuery(). While ZF2 supports query string parameters, route parameters are distinct. They exist in the request URI path, not the query string. Accessing them via $_GET will return null unless you explicitly append them as query strings (e.g., /my-article?slug=my-article), which defeats the purpose of segment routing. Always prefer injecting the parameter directly into the action method.
If you need to access this data without modifying the controller signatureâfor instance, in a factory or a service layer injected by the routerâyou can retrieve it from the request object using params()->fromRoute(). This is useful when passing the slug to a model or service class:
public function indexAction()
{
$slug = $this->params()->fromRoute('slug', 'home');
// Proceed with business logic using $slug
}
This approach separates concerns effectively, keeping your controller actions clean while ensuring the dynamic data flows correctly from the router to your application logic. By leveraging ZF2's native parameter binding, you maintain a robust separation between URL structure and business logic without relying on fragile global state or query string manipulation.