how to pass parameter on redirect in yii

how to pass parameter on redirect in yii

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

Stefan Bogdanescu

Founder & Senior Architect

Passing Parameters on Redirect in Yii

When handling a successful database insertion, it is common practice to redirect users immediately to another controller action rather than reloading the current page. This prevents duplicate form submissions and improves user experience. In Yii, this is typically achieved using $this->redirect($this->createUrl('controller/action')). However, developers often encounter a specific limitation: unlike the render method, which accepts an associative array to pass variables to a view, the standard redirect does not natively support passing parameters directly in its signature.

To understand why this distinction exists, you must look at how Yii handles HTTP responses versus view rendering. The $this->redirect() method issues an HTTP 302 status code and sets the Location header. This header contains a raw URL string that the browser interprets. While it is technically possible to append query string parameters to that URL manually, relying on a helper method that expects only a controller/action pair can feel restrictive when you need to pass data like IDs, filters, or messages back to the destination view.

The standard workaround involves constructing a URL with a query string appended directly. Instead of passing an array of variables, you build a string representation of your parameters using http_build_query(). This function takes an associative array and returns a properly encoded query string. You can then concatenate this string to the base URL generated by createUrl() or url().

Here is how you would implement this pattern:

// Instead of trying to pass data directly to redirect
// $this->redirect($this->createUrl('user/show', ['id' => $userId])); // This will not work as expected for passing arbitrary view data

// You construct the URL with a query string manually
$params = [
    'filter_type' => 'new',
    'message'     => 'Record created successfully',
];

$url = $this->createUrl('user/show') . '?' . http_build_query($params);

$this->redirect($url);

In this example, the destination controller action receives these parameters as $_GET variables. You can access them in your view file just like you would with any other GET request:

// Inside the user/show view or controller logic
$type = Yii::$app->request->get('filter_type');
$message = Yii::$app->request->get('message');

This approach is robust because it leverages standard HTTP protocols. However, there are significant trade-offs to consider. Passing data via GET parameters limits the payload size and exposes sensitive information in the browser's address bar. If you need to pass large datasets or confidential data, this method is unsuitable. Furthermore, if the destination action expects POST data but receives GET data instead, your logic might break unless explicitly handled.

An alternative strategy often employed in Yii applications is using Flash messages. This decouples the parameter passing from the URL itself. After insertion, you set a session flash message and redirect without parameters:

$this->session->setFlash('success', 'Record created successfully');
$this->redirect($this->createUrl('user/show'));

Then, in the destination view or controller, you check for the flash before displaying content. This keeps URLs clean but requires rendering logic to be aware of potential messages.

Ultimately, the choice between constructing a URL with query parameters and using Flash depends on your specific use case. If you need to pass a specific ID for navigation, query parameters are the correct tool. If you are merely passing a success message or filter state that doesn't strictly belong in the URL, session flash is often cleaner. Both methods are standard practices within the Yii ecosystem and ensure your application remains maintainable and predictable.

Our network