M
M
Maxim Osadchy2017-10-09 19:12:42
Yii
Maxim Osadchy, 2017-10-09 19:12:42

How to add additional content to a page that is pulled from the database?

There is a page controller:

class PageController extends AppController
{
    public function actionView($alias)
    {
        $page = Page::find()->where(['alias' => $alias])->one();
        if(empty($page)){
            throw new HttpException(404, 'Страница не найдена.');
        }
        return $this->render('view', compact(['page']));
    }
}

For example, I want to add a feedback form to a certain page, how can I implement it - write a separate render for a specific page, combining information from the database and new data, or in some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-10-09
@slo_nik

Good evening.
Alternatively, you can do this:

class PageController extends AppController
{
    public function actionView($alias)
    {
       
        $page = Page::find()->where(['alias' => $alias])->one();
        if(empty($page)){
            throw new HttpException(404, 'Страница не найдена.');
        }

        $contact = new ContactForm();
        return $this->render('view', ['page' => $page, 'contact' => $contact]);
    }
}

Move it to a separate method, there will be less code in action
$page = Page::find()->where(['alias' => $alias])->one();
        if(empty($page)){
            throw new HttpException(404, 'Страница не найдена.');
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question