A
A
Andrey Novoselov2022-04-17 09:25:29
PHP
Andrey Novoselov, 2022-04-17 09:25:29

MVC php how to pass title from view?

How do I pass the title from the view to the main template with $this->title = 'Title'; how it is done in yii2. And just how easy is it to pass data to the view?

This is how I'm putting together the page so far. Maybe here, too, tell me what can be finalized with a file

public function createView($path, $layout = 'index') {
        if(file_exists($path)) {
            require_once $_SERVER["DOCUMENT_ROOT"] . '/core/views/layout/' . $layout . '.php';
            require_once $path;
        } else {
            View::pageNotFound();
        }  
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2022-04-17
@vilinyh

It is not very clear "who stood on whom", but the piece of code above is most likely more convenient to bring to the form:

public function createView($path, $layout = 'index')
{
    if(!file_exists($path)) {
        View::pageNotFound();
    }

    $this->layout = $layout;
    $someParamsToSendIntoTheView = [...];

    return $this->render($path, $someParamsToSendIntoTheView);
}

To pass parameters from View $path inside to $layout use View::$params ( https://www.yiiframework.com/doc/api/2.0/yii-base-... ): , inside layout will become available (see on the breadcrumbs example, which is generated by gii to all pages by default). $this->params['my-parameter'] = 'Some Data';$this->params['my-parameter']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question