Yii 2 FAQ

Then we tell it what attributes we want in the array. In this case, since we are not using this for backend record creation and maitenance, we can include fewer attributes. In this case, we only need:


'attributes' => ['question', 'answer', 'faq_value'], 

We are also setting page size for pagination to 10. So this is pretty simple stuff.

Next we need to create a frontend controller for Faq, that uses the backend Faq model and backend FaqSearch model. We’re only going to have 2 actions view and index. There is no need for create, update or delete actions because they are handled in the backend by admin, not in the frontend by users. Because these are FAQs, we want them visible to all users, so we won’t be restricting access, so this makes the controller fairly simple.

Here is the entire controller:


<?php
  
namespace frontend\controllers;

use Yii;
use backend\models\Faq;
use backend\models\search\FaqSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * FaqController implements the CRUD actions for Faq model.
 */
class FaqController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Faq models.
     * @return mixed
     */
    public function actionIndex()
    {
       
      $searchModel = new FaqSearch(); 
        
      $provider = $searchModel->defaultProvider();
      
      return $this->render('index', [
            'searchModel' => $searchModel,
            'provider' => $provider,
        ]);
      
        
    }

    /**
     * Displays a single Faq model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
         
      protected function findModel($id)
    {
        if (($model = Faq::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
}

It’s really very simple. On the index method, we call a new instance of FaqSearch model, which allows us to set an instance of $searchModel->defaultProvider(); to $provider, which we can pass to the view. So now the view will have access to the ArrayDataProvider that we made, with the sort by ascending value on faq_value.

In the view, to display this, we need the following in frontend\views\faq\index.php:


<?php
   $data = $provider->getModels();
   $questions = Arrayhelper::map($data, 'question', 'id');
   foreach ($questions as $question => $id){
       
        $url = Url::to(['faq/view', 'id'=>$id]);
        $options = [];
        echo '<div class="panel-body">'. Html::a($question, $url, $options) .'</div>'; 
       
   }
             
?>

So now we’re setting $provider->getModels() to $data. If you look at the guide, this is the method they use in their example, so that is exactly what I used. Then I created key value pairs out of question and Id by using the ArrayHelper::map method. If you try this with faq records that have different faq_value settings, you will see that it respects the ordering by faq_value. So you can absolutely hand control over the order to your client by providing backend UI that lets them set the faq_value.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s