Quadri di sviluppo
Framework PHP leggero con un ingombro ridotto per la creazione di applicazioni web
Small footprint (2MB), minimal overhead, and exceptional performance with only essential components loaded
Nearly zero-config setup with sensible defaults - start coding immediately without complex configuration
Loosely follows MVC pattern with freedom to adapt to your needs - use full MVC or simplified approach
XSS filtering, CSRF protection, password hashing, input validation, and SQL injection prevention
Rich set of libraries for database, email, sessions, forms, file uploads, pagination, and more
Clear, comprehensive user guide with practical examples and active community support
sudo apt update
sudo apt install php8.1 php8.1-{fpm,cli,mysql,mbstring,xml,curl,intl,zip,gd} -y2. Install Composer:curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer3. Install CodeIgniter via Composer:cd /var/www
composer create-project codeigniter4/appstarter myproject
cd myproject4. Set Permissions:sudo chown -R www-data:www-data /var/www/myproject
sudo chmod -R 755 /var/www/myproject
sudo chmod -R 777 /var/www/myproject/writable5. Configure Environment:cp env .env
nano .envSet configuration:CI_ENVIRONMENT = development
database.default.hostname = localhost
database.default.database = mydb
database.default.username = dbuser
database.default.password = dbpass
database.default.DBDriver = MySQLi6. Configure Apache Virtual Host:sudo nano /etc/apache2/sites-available/myproject.confAdd:<VirtualHost *:80>
ServerName myproject.local
DocumentRoot /var/www/myproject/public
<Directory /var/www/myproject/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/myproject_error.log
CustomLog ${APACHE_LOG_DIR}/myproject_access.log combined
</VirtualHost>Enable site:sudo a2ensite myproject.conf
sudo a2enmod rewrite
sudo systemctl restart apache27. Or Configure Nginx:sudo nano /etc/nginx/sites-available/myprojectAdd:server {
listen 80;
server_name myproject.local;
root /var/www/myproject/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}Enable:sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx8. Install MySQL and Create Database:sudo apt install mysql-server -y
sudo mysql
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbpass';
GRANT ALL PRIVILEGES ON mydb.* TO 'dbuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;9. Run Migrations (if using):php spark migrate10. Test Installation:php spark serve
# Visit http://localhost:8080
# Environment
CI_ENVIRONMENT = production # development, testing, production
# Base URL
app.baseURL = 'https://yourdomain.com/'
# Database
database.default.hostname = localhost
database.default.database = mydb
database.default.username = dbuser
database.default.password = secure_password
database.default.DBDriver = MySQLi
database.default.DBPrefix =
# Encryption
encryption.key = your-encryption-key-here # Generate: php spark key:generate2. Routes Configuration (app/Config/Routes.php):<?php
$routes->get('/', 'Home::index');
$routes->get('blog', 'Blog::index');
$routes->get('blog/(:segment)', 'Blog::view/$1');
// RESTful API routes
$routes->resource('api/users');
// Group routes
$routes->group('admin', ['filter' => 'auth'], function($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
$routes->resource('posts');
});3. Database Configuration (app/Config/Database.php):<?php
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydb',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => true,
'charset' => 'utf8mb4',
'DBCollat' => 'utf8mb4_general_ci',
];4. Controller Example:<?php
namespace App\Controllers;
class Blog extends BaseController
{
protected $blogModel;
public function __construct()
{
$this->blogModel = model('BlogModel');
}
public function index()
{
$data = [
'posts' => $this->blogModel->paginate(10),
'pager' => $this->blogModel->pager
];
return view('blog/index', $data);
}
public function view($slug)
{
$post = $this->blogModel->where('slug', $slug)->first();
if (!$post) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('blog/view', ['post' => $post]);
}
}5. Model Example with Validation:<?php
namespace App\Models;
use CodeIgniter\Model;
class BlogModel extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
protected $allowedFields = ['title', 'slug', 'body', 'author_id'];
protected $useTimestamps = true;
protected $validationRules = [
'title' => 'required|min_length[3]|max_length[255]',
'slug' => 'required|is_unique[posts.slug]',
'body' => 'required'
];
protected $validationMessages = [
'title' => [
'required' => 'Title is required',
'min_length' => 'Title must be at least 3 characters'
]
];
}6. Form Validation:<?php
// In controller
public function create()
{
$validation = \Config\Services::validation();
$validation->setRules([
'email' => 'required|valid_email|is_unique[users.email]',
'password' => 'required|min_length[8]',
'confirm' => 'required|matches[password]'
]);
if (!$validation->withRequest($this->request)->run()) {
return view('register', ['validation' => $validation]);
}
// Save user
}7. Working with Database (Query Builder):<?php
// Select
$users = $db->table('users')
->select('id, name, email')
->where('status', 'active')
->orderBy('name', 'ASC')
->get()
->getResultArray();
// Insert
$data = ['name' => 'John', 'email' => 'john@example.com'];
$db->table('users')->insert($data);
// Update
$db->table('users')
->where('id', 1)
->update(['status' => 'inactive']);
// Delete
$db->table('users')->where('id', 1)->delete();
// Join
$results = $db->table('posts')
->join('users', 'users.id = posts.author_id')
->get()
->getResult();8. Sessions and Authentication:<?php
// Start session
$session = session();
// Set session data
$session->set('user_id', 123);
$session->set('logged_in', true);
// Get session data
$userId = $session->get('user_id');
// Flash data (available only on next request)
$session->setFlashdata('success', 'Login successful!');
// Destroy session
$session->destroy();9. Caching:<?php
$cache = \Config\Services::cache();
// Save to cache
$cache->save('user_123', $userData, 3600); // 1 hour
// Get from cache
$data = $cache->get('user_123');
// Delete from cache
$cache->delete('user_123');
// Clear all cache
$cache->clean();10. CLI Commands (Spark):# Run migrations
php spark migrate
# Rollback migrations
php spark migrate:rollback
# Create migration
php spark make:migration CreateUsersTable
# Create controller
php spark make:controller Blog
# Create model
php spark make:model BlogModel
# Run seeder
php spark db:seed UserSeeder
# Clear cache
php spark cache:clearBest Practices:Iniziate in pochi minuti con il nostro semplice processo di distribuzione VPS
Distribuisci in circa 2 minuti • Accesso totale root • Supporto tramite ticket ed email