xCRUD & CodeIgniter 4.

xCRUD integrates easily with CodeIgniter 4.
Just follow the instructions below.

CodeIgniter Helper

Create the file Xcrud_helper.php in your CI helpers directory with the code below.

  <?php 
  require_once FCPATH . 'assets/vendor/xcrud/xcrud.php';
  
  /**
  * Include xcrud libraries
  */
  if ( ! function_exists('get_xcrud'))
  {
    function get_xcrud($name = false){
      return Xcrud::get_instance($name);
    }
  }
  
  /**
  * Get html <header> css <link> tags for xcrud & its plugins.
  * This function can only get called after calling get_xcrud()
  */
  if ( ! function_exists('get_xcrud_css'))
  {
    function get_xcrud_css(){
      return Xcrud::load_css();
    }
  }
  
  /**
  * Get html <script> tags for xcrud & its plugins.
  * This function can only get called after calling get_xcrud()
  */
  if ( ! function_exists('get_xcrud_js'))
  {
    function get_xcrud_js(){
      return Xcrud::load_js();
    }
  }

  /**
   * Set Xcrud_config properties
   * @param string $k Xcrud_config static property
   * @param string $v the value to assign the Xcrud_config static property
   */ 
  if ( ! function_exists('set_xcrud_config'))
  {
    function set_xcrud_config($k, $v){
      Xcrud_config::${$k} = $v;
    }
  }
  
  
  /* End of file Xcrud_helper.php */

Copy xCRUD to the directory public/assets/vendor/xcrud/ or change the line below to reflect your change.

  require_once FCPATH . 'assets/vendor/xcrud/xcrud.php';
The helper function get_xcrud() returns an instance of xCRUD. Optionally, it returns a named xCRUD instance if supplied as a parameter.

Helper functions get_xcrud_css() and get_xcrud_js() give you manual control over where to place xCRUD's css and js output respectively. This will be made clear when calling CodeIgniter's view further below.

xCRUD Configuration

The following xcrud_config.php settings are important to ensuring proper function.

  • Database settings: Remember to set the correct values for the items below.
    • $dbname
    • $dbuser
    • $dbpass
    • $dbhost
  • Theme settings: This demo uses bootstrap 4.5 so the settings below are set as shown.
    • $theme = 'bootstrap'
    • $load_bootstrap = false
    • $load_bootstrap4 = true
  • Set $manual_load = true to disable xCRUD's automatic css and js output.

CodeIgniter Controller

Create the file Demo.php in your CI controllers directory with the code below.

  <?php
  namespace App\Controllers;

  class Demo extends BaseController {

    protected $helpers = ['xcrud'];

    public function index()
    {
      $xcrud = get_xcrud();
      $xcrud->table('orders');
      
      $data['content'] = $xcrud->render('create');
      
      return view('some_content_simple', $data);
    }
  }

Use the class property $helpers to load the xCRUD helper (Xcrud_helper.php) we just created. Whenever the controller is loaded this helper will be automatically loaded into memory so that you can use its methods anywhere inside the controller.

Function index() uses the xCRUD helper's function get_xcrud() which calls Xcrud::get_instance() and returns the xCRUD instance. You can optionally name the xCRUD instance by passing a string to get_xcrud(). You can check docs for further information.

  $xcrud = get_xcrud();

Now we can call our standard xCRUD methods from the instance assigned to $xcrud.

  $xcrud->table('orders');      

Load the table: orders.

  $data['content'] = $xcrud->render('create');   

Render the table in 'create' mode and assign its output to our $data array.

    return view('some_content', $data);

Pass this array to our view (below) for output to the browser.

CodeIgniter View

Create the file some_content.php in the app/Views/ directory with the code below. This is the view being loaded in the index() method of our controller above by view('some_content', $data)

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>xCRUD & CodeIgniter 4</title>

  <?php echo get_xcrud_css(); ?>
</head>
<body>
  <?php echo $content; ?>
  <?php echo get_xcrud_js(); ?>
</body>
</html>

Remember we set xCRUD $manual_load = true? We are now able to call our helper functions <?php echo get_xcrud_css(); ?> and <?php echo get_xcrud_js(); ?> to load our css and js respectively, where we please.

We rendered the table to $data['content'] array. CodeIgniter extracts it as $content which we echo in our <body> tag here <?php echo $content; ?>.

Remember to configure your routes $routes->get('demo', 'Demo::index'); and assuming you removed index.php from the url...

Run your app: example.com/demo

That's all. Happy coding.