Load view file in controllers in codeigniter 4

Deepak Tailor Image
Deepak Tailor - Sep 30 2020
Load view file in controllers in codeigniter 4

The view file is a web page, then the user is shown by the controllers. Such as header, footer, sidebar etc. We cannot call the views file directly. It is set from the controller to show which view file.

Load Single view file:

You can use the view function to load or display any view file.

echo view('home');
echo view('about');
echo view('service');
echo view('contact');
how to load multiple view files

If you want to load multiple views file, then you cannot do that, these multiple files are interlinked.

echo view('header'); // header view file
echo view('menu'); // menu view file
echo view('content', $data); // content view file
echo view('footer'); // footer view file
view file with directory
echo view('home/login');
echo view('home/register');
echo view('users/dashboard');
pass dynamic data controller to view files

We can pass the data as another parameter in the view function. Through this medium we pass the view file data from the controller.

$data = [
	'title'   => 'My title',
	'heading' => 'My Heading',
	'message' => 'My Message'
];
// data pass in view file
echo view('blogview', $data);
public function index()
{
    $data['title']   = "My Real Title";
    $data['heading'] = "My Real Heading";

    // data pass in view file
    echo view('blogview', $data);
}

// show heading in view file 
<h1><?= $heading ?></h1>
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry for the last 5 years. For professional and customize web development, you can send inquiry on our email.
----
You can contact him at support@fulstackdev.com