M
M
ms23452015-12-29 08:03:14
CodeIgniter
ms2345, 2015-12-29 08:03:14

How to connect the views I need automatically?

Good time.
The procedure for connecting views in the controller now looks like this:

$this->load->view("document_top"); //Тут doctype и начала контейнера html
$this->load->view("document_head"); //Тут контейнер head и начало тега body
$this->load->view("users");
$this->load->view("document_bottom"); //Тут конец контейнеров body и html

Question: how would it be possible to automatically implement the connection of the view in the controller before and after its execution, in order to do without connecting these views every time.
I tried to change the parent CI_Controller (file: /system/core/contaller.php ), namely to connect the "document_top" view to "__construct", and the "document_bottom" view to "__destruct". However, nothing comes out when connecting to "__destruct". Error appears:

Warning: include(application/errors/error_php.php) [function.include]: failed to open stream: No such file or directory in Z:\OpenServer\domains\local.net\system\core\Exceptions.php line 183
Warning: include(application/errors/error_php.php) [function.include]: failed to open stream: No such file or directory in Z:\OpenServer\domains\local.net\system\core\Exceptions.php on line 183
Warning: include() [function.include]: Failed opening 'application/errors/error_php.php' for inclusion (include_path='.;z:/openserver/modules/php/PHP-5.3;z:/openserver/modules/ php/PHP-5.3/PEAR/pear') in Z:\OpenServer\domains\local.net\system\core\Exceptions.php on line 183

However, if the connection in "__destruct" is replaced with the output:
echo "</body></html>";
Then everything works as it should. However, I need to include the file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmitriy, 2015-12-29
@dmitriylanets

in no case can you edit the framework classes directly, create the base class MY_Controller.php in it, connect the necessary views, as an option, create a method in MY_Controller

protected function renderPage($page,$data = []) {
$html = $this->load->view("document_top",[],true); 
$html .=$this->load->view("document_head",[],true); 
$html .= $this->load->view($page,$data,true);
$html .= $this->load->view("document_bottom",[],true); 
$this->output->set_output( $html );
}

then use your controllers like this:
class User extends MY_Controller {
public function Index(){
$userlist = [];
// logic
$this->renderPage('users',[
'userlist' => $userlist
]);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question