07-20-2023, 01:30 PM
Creating the Controller for CodeIgniter
Create a phpMyAdmin Table
Create the Model
To create a route using the controller, this way whenever a user makes a request, the view will be supplied with the information from the model.
- To create a new controller, go to the app/Controllers directory and create a file named Agencies.php. Remember, CodeIgniter's controllers must have the same name as the class inside it.
- Paste the following code into the file:
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\AgencyModel;
class Agencies extends Controller
{
}
- We'll create a simple function to sort all the database entries from highest to lowest. To do so, add this code snippet to the file:
public function index()
{
$model = new AgencyModel();
$data['agencies'] = $model->orderBy('id', 'DESC')->findAll(); return view('agencies', $data);
}
- The complete code will look like this:
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\AgencyModel;
class Agencies extends Controller
{
public function index()
{
$model = new AgencyModel();
$data['agencies'] = $model->orderBy('id', 'DESC')->findAll(); return view('agencies', $data);
}
}

