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.