We will create first program in CodeIgniter 4. So, we need a Controller file, a view file and a route.
Create Controller
Open project setup into terminal and run this spark command into it.
$ php spark make:controller Site --suffix
It will create SiteController.php inside /app/Controllers folder. Open file and write this code.
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
class SiteController extends BaseController
{
public function index()
{
return view("site-view");
}
}We have done with the controller.
Next, create a view file.
Create View File
Create site-view.php inside /app/Views folder.
Open site-view.php and write this code into it.
<h2>Hi, This is my first page in CodeIgniter 4</h2>
Now, we need a route.
Create Route
Open Routes.php from /app/Config folder. Open file and add this route into it.
//...
$routes->get("/first-page", "SiteController::index");
Finally, everything is perfect.
Start Development Server
Back to project terminal and run this spark command.
$ php spark serve
It will start application in development server at 8080 port.
Open browser, URL – http://localhost:8080/first-page
Output
Hi, This is my first page in CodeIgniter 4Read more