0% found this document useful (0 votes)
14 views3 pages

Codeigniter 4 Cheatsheet

The CodeIgniter 4 Cheatsheet provides essential information on the framework's core concepts, including MVC structure, configuration settings, and directory structure. It covers creating controllers, routing, models, database interactions, and views, along with code snippets for quick reference. This resource is designed to accelerate development by summarizing key functionalities and best practices in CodeIgniter 4.

Uploaded by

lionelagbavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Codeigniter 4 Cheatsheet

The CodeIgniter 4 Cheatsheet provides essential information on the framework's core concepts, including MVC structure, configuration settings, and directory structure. It covers creating controllers, routing, models, database interactions, and views, along with code snippets for quick reference. This resource is designed to accelerate development by summarizing key functionalities and best practices in CodeIgniter 4.

Uploaded by

lionelagbavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CodeIgniter 4 Cheatsheet

A comprehensive cheat sheet for CodeIgniter 4, covering essential concepts, configurations, and code snippets to accelerate development.

Core Concepts & Configuration


MVC Structure Configuration (app/Config/[Link])
Model: Represents data and business logic. $baseURL The base URL of your application (e.g.,
[Link] )
View: Handles the presentation of data to the user (HTML, etc.).
Controller: Manages requests, interacts with models, and loads views. $indexPage The name of your index page (usually [Link] or empty
string to remove it.)
Directory Structure: app , system , public , writable
$encryption Encryption key for secure sessions and data.
app - Application specific code (controllers, models, views, etc.) Key

system - Core CodeIgniter files (do not modify). $defaultLoc The default locale for your application (e.g., en , fr ).
public - Contains the [Link] file and assets (CSS, JS, images). ale

writable - Directory for logs, cache, and uploads. $sessionDri The session driver to use ( FileHandler ,
ver DatabaseHandler , etc.).

$timezone The timezone for your application (e.g.,


America/Los_Angeles ).

Controllers & Routing


Creating a Controller Routing Request Object
Create a file in app/Controllers/ (e.g., app/Config/Rout Configuration file for Accessing the request object in a controller:
[Link] ) [Link] defining routes.
$request = \Config\Services::request();
$routes- Defines a GET route. $data = $request->getPost(); // Get POST
<?php
>get('path', data
'Controller::met
$data = $request->getGet(); // Get GET
namespace App\Controllers;
hod');
data
$routes- Defines a POST route.
use CodeIgniter\Controller;
>post('path',
$userAgent = $request->getUserAgent();
'Controller::met
class MyController extends Controller $ipAddress = $request->getIPAddress();
hod');
{
$routes- Creates RESTful resource
public function index()
>resource('photo routes for a controller
{
s'); named ‘Photos’.
return 'Hello, World!';
}
Route Parameters $routes-
>get('users/(:num)',
}
'Users::show/$1');

Access the controller via URL (e.g., Auto Route Set $routes-
[Link] ) (Improved) >setAutoRoute(true); to
automatically route based
on controller and method
names.

Page 1 of 3 [Link]
Models & Database
Creating a Model Database Configuration Database Queries
(app/Config/[Link])
Create a file in app/Models/ (e.g.,
$db = \Config\Database::connect();
[Link] ) $defau Default database connection settings.
lt
<?php // Simple query
hostna Database server hostname (e.g., $query = $db->query('SELECT * FROM
me localhost ).
namespace App\Models; users');
userna Database username.
me foreach ($query->getResult() as $row) {
use CodeIgniter\Model;
passwo Database password. echo $row->name;

class UserModel extends Model rd }

{ databa Database name.


protected $table = 'users'; se // Using Query Builder
$builder = $db->table('users');
protected $primaryKey = 'id';
DBDriv Database driver (e.g., MySQLi ,
$builder->select('id, name, email');
er Postgre , SQLite3 ).
protected $useAutoIncrement = true; $query = $builder->get();

protected $returnType = 'array'; // Insert data


protected $useSoftDeletes = true; $data = [
'name' => 'John Doe',

protected $allowedFields = ['name', 'email' => '[Link]@[Link]',


'email']; ];
$builder->insert($data);

protected $useTimestamps = true;


protected $createdField = // Update data
'created_at'; $data = ['email' =>

protected $updatedField = '[Link]@[Link]'];


'updated_at'; $builder->where('id', 1);
protected $deletedField = $builder->update($data);

'deleted_at';

protected $validationRules = [];

protected $validationMessages = [];


protected $skipValidation =
false;

Views & Templating


Creating a View Loading Views in Controller
Create a file in app/Views/ (e.g.,
<?php
welcome_message.php )

<!DOCTYPE html> namespace App\Controllers;

<html>
<head> use CodeIgniter\Controller;

<title>Welcome</title>

</head> class Home extends Controller

<body> {

<h1>Hello, <?php echo $name; ?>! public function index()

</h1> {

</body> $data = ['name' =>

</html> 'CodeIgniter'];
return view('welcome_message',
$data);
}
}

Page 2 of 3 [Link]
Templating with View Layouts
Create a layout file (e.g.,
app/Views/layouts/[Link] ):
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?= $this->renderSection('content')

?>
</body>
</html>

Create a view that extends the layout (e.g.,


app/Views/[Link] ):

<?= $this->extend('layouts/default') ?>

<?= $this->section('content') ?>


<h1>Welcome Home!</h1>

<?= $this->endSection() ?>

In Controller:
public function index()
{
$data['title'] = 'Home Page';
return view('home', $data);
}

Page 3 of 3 [Link]

You might also like