Difference between #2 and #4 of
An easy way to display a success page using flash messages

Changes

Title unchanged

An easy way to display a success page using flash messages

Category unchanged

Tips

Yii version unchanged

Tags unchanged

flash message, Forms

Content changed

[...]
So what are new stuff here?

* First, instead of saving a string as flash message, we save an array of data that will persist to the next page request.

* Second, we use the action code to control which view to render. The code conforms better to the MVC pattern.

 
> Note: Flash messages are essentially stored as session data. As a result, they will be serialized when saving and deserialized when loading. For this reason, the data saved as flash message should be simple data, such as strings, numbers. Do not store objects (e.g. data model objects) or resource handles in it because their serialization and deserialization may cause problem.
 
 
 
## Refactoring the code
 
 
If an application needs to display different success pages in different actions, we may refactor the above code so that the usage is even simpler and more DRY.
 
 
First in the [base controller class](http://www.yiiframework.com/wiki/121/extending-common-classes-to-allow-better-customization), define the following method:
 
 
 
```php 
/**
 
 * Renders a success view.
 
 * Note that this method will redirect the browser to the 'site/success' route
 
 * which will then render the specified success view.
 
 * @param string $view the view name. It can be a view relative to
 
 * the current controller or an absolute view (starting with '/')
 
 * @param array $data the data to be passed to the view.
 
 */
 
public function success($view, $data=array())
 
{
 
if($view[0]!=='/')  // relative to current controller
 
$view = '/' . $this->id . '/' . $view;
 
$data['_view_'] = $view;
 
Yii::app()->user->setFlash('_success_', $data);
 
$this->redirect(array('site/success'));
 
}
 
```
 
 
Then, in the `SiteController` class, define the following `success` action:
 
 
 
```php 
/**
 
 * Displays a success page.
 
 * The success page content is specified via the flash data '_success_'
 
 * which is generated by {@link Controller::success()} method.
 
 * If the flash data does not exist, it will redirect the browser to the homepage.
 
 */
 
public function actionSuccess()
 
{
 
if (!Yii::app()->user->hasFlash('_success_'))
 
$this->redirect(Yii::app()->homeUrl);
 
$data = Yii::app()->user->getFlash('_success_');
 
$view = $data['_view_'];
 
unset($data['_view_']);
 
$this->render($view, $data);
 
}
 
```
 
 
Now, the previous `actionRegister()` method can be greatly simplified:
 
 
 
```php 
public function actionRegister()
 
{
 
$model = new User('register');
 
if (isset($_POST['User']))
 
{
 
$model->attributes = $_POST['User'];
 
if ($model->save())
 
{
 
$this->success('registerSuccess', array(
 
'username' => $model->username,
 
'email' => $model->email,
 
));
 
}
 
}
 
$this->render('register', array('model'=>$model));
 
}
 
```
 
 
And we can call the `success()` method in any other action as long as it needs to display a success page.
 
21 0
29 followers
Viewed: 56 616 times
Version: 1.1
Category: Tips
Written by: qiang
Last updated by: qiang
Created on: Apr 11, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history