Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating the Controller for CodeIgniter
#1

    Creating the Controller for CodeIgniter
   

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.


   
           
  1. 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.
  2.        
  3. Paste the following code into the file:
  4.    
   
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\AgencyModel;
class Agencies extends Controller
{
}
   
   
           
  1. 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:
  2.    
   
public function index()
{   
  $model = new AgencyModel();
  $data['agencies'] = $model->orderBy('id', 'DESC')->findAll();    return view('agencies', $data);
}
   
   
           
  1. The complete code will look like this:
  2.    
   
<?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);
}
}
   

   


Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)