Codeigniter 4 Create Controller, Model, View Example

Codeigniter 4 Create Controller, Model, View Example

To create controller, model, and views in codeIgniter 4 using command or manually; Simply use make:controller, or make:model command to create controller using the command, and you can also navigate to app/controller or model folder and create manually controller and model files into it.

Controllers act as glue code, marshaling data back and forth between the view (or the user that’s seeing it) and the data storage.

Models manage the data of the application and help to enforce any special business rules the application might need.

Views are simple files, with little to no logic, that displays the information to the user.

Create a Controller in CodeIgniter 4

To use php spark make:controller ControllerName command on command line or terminal to controller in codeigniter:

php spark make:controller ControllerName

All Controllers are typically saved in /app/Controllers. To create a new controller in codeigniter 4, simply navigate to app/controller folder and create a new php file and add the following controller code into it:

<?php namespace App\Controllers;

use CodeIgniter\Controller;

class Blog extends Controller
{
        public function index()
        {
                echo 'Hello World!';
        }
}

Create a Model in CodeIgniter 4

To use php spark make:model ModelName command on command line or terminal to model in Codeigniter:

php spark make:model ModelName

All Models are typically stored in /app/Models. To create a new model in CodeIgniter 4 manually. So go to /app/Models, and create a new PHP file and update the following code:

<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{

}

CodeIgniter 4 Create a View

All views are typically saved in /app/Views. If you want to create a new views in CodeIgniter 4. So go to /app/Views and create a new PHP file and update the following code:

<html>
<head>
    <title>My Blog</title>
</head>
<body>
    <h1>Welcome to my Blog!</h1>
</body>
</html>

Conclusion

In this tutorial, you have learned how and where to create controllers, models, and views in CodeIgniter 4 framework.

Recommended Codeigniter Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *