Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
Home » Codeigniter 4 »
What is Codeigniter View?
A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc.
Views are never called directly, they must be loaded by a controller.
File name : index.php
Creating a View
File name : index.php
<html>
<head>
<title>My Ittutorial</title>
</head>
<body>
<h1>Welcome Sana!</h1>
</body>
</html>
Then save the file in your app/Views directory.
Displaying a View
File name : index.php
echo view('view_name');
view() method is load and display a particular view file.
File name : index.php
<?php
namespace App\Controllers;
class Itechxpert extends \CodeIgniter\Controller
{
public function index()
{
echo view('trendenews');
}
}
Loading Multiple Views
CodeIgniter handle multiple calls to view() from within a controller.
File name : index.php
<?php
namespace App\Controllers;
class Itechxpert extends \CodeIgniter\Controller
{
public function index()
{
$data = [
'page_title' => 'Your title'
];
echo view('header');
echo view('menu');
echo view('content', $data);
echo view('footer');
}
}
Views within Sub-directories
File name : index.php
echo view('directory_name/file_name');
Namespaced Views
File name : index.php
echo view('Example\Blog\Views\BlogView');
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array in the second parameter of the view function.
File name : index.php
$data = [
'title' => 'Itechxpert',
'First_Name' => 'Sana',
'Last_Name' => 'Mahtab'
];
echo view('myview', $data);
Example :-
File name : index.php
<?php
namespace App\Controllers;
class Itechxpert extends \CodeIgniter\Controller
{
public function index()
{
$data['heading'] = 'MyHeading';
$data = [
'title' => 'Itechxpert',
'First_Name' => 'Sana',
'Last_Name' => 'Mahtab'
];
echo view('myview', $data);
}
}
Now open your view file and change the text to variables that correspond to the array keys in your data:
File name : index.php
<head>
<title><?= $title ?></title>
</head>
<body>
<h1><?= $heading ?></h1>
<h2><?php echo $First_Name;?> </h2>
</body>
</html>
Creating Loops
File name : index.php
namespace App\Controllers;
class Itechxpert extends \CodeIgniter\Controller
{
public function index()
{
$data = [
'todo_list' => ['House', 'Call Mom', 'Run Errands'],
'title' => "My Title",
'heading' => "My Heading"
];
echo view('blogview', $data);
}
}
File name : index.php
<head>
<title><?= $title ?></title>
</head>
<body>
<h1><?= $heading ?></h1>
<h3>My Todo List</h3>
<ul>
<?php foreach ($todo_list as $item):?>
<li><?= $item ?></li>
<?php endforeach;?>
</ul>
<em>© 2021</em>
</body>
</html>