slogan3

Help me met het implementeren van CodeIgniter CLI-commando's voor efficiënte automatisering

description

Met deze prompt kan ik efficiënte automatiseringstools bouwen binnen CodeIgniter, waardoor ik tijd bespaar op routinetaken en minder fouten maak. Dit verbetert mijn ontwikkelproces door het integreren van herbruikbare, configureerbare CLI-commando's die specifiek zijn afgestemd op mijn projectbehoeften.

prompt

Help me bij het ontwerpen en implementeren van aangepaste CLI-commando's in mijn CodeIgniter-project om taken zoals database-migraties, cache-clearance en batchprocessen te automatiseren. Beschrijf de be ...

try_prompt

generate_helper
disclaimerOnPageApi

ai_answers

provider: openai
model: gpt-4.1-nano
Natuurlijk! Hieronder vind je een stapsgewijze handleiding voor het toevoegen van aangepaste CLI-commando's in je CodeIgniter 3.1.11-project, gericht op database-migraties en cache legen. 1. **Structuur en aanpak** In CodeIgniter 3.1.11 kun je aangepaste CLI-commando’s maken door gebruik te maken van controllers die via de command-line kunnen worden aangeroepen. Het is gebruikelijk om een speciale controller te maken in de map `application/controllers/cli/`. 2. **Aanmaken van een CLI-controller** Maak een nieuwe controller, bijvoorbeeld `Tasks.php`, in `application/controllers/cli/`. ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Tasks extends CI_Controller { public function __construct() { parent::__construct(); // Laad benodigde helpers/libraries $this->load->helper('file'); // voor cache verwijderen $this->load->library('migration'); // voor migraties } // Database migraties uitvoeren public function migrate() { if (!$this->migration->latest()) { echo "Migratie mislukt: " . $this->migration->error_string() . "\n"; } else { echo "Migratie succesvol uitgevoerd.\n"; } } // Cache legen public function clear_cache() { $cache_dir = WRITEPATH . 'cache/'; if (delete_files($cache_dir, TRUE)) { echo "Cache succesvol verwijderd.\n"; } else { echo "Fout bij verwijderen cache.\n"; } } } ?> ``` 3. **Aanpassen van `config/migration.php`** Zorg dat migraties correct zijn ingesteld. Bijvoorbeeld: ```php $config['migration_enabled'] = TRUE; $config['migration_path'] = 'migrations/'; $config['migration_type'] = 'timestamp'; // of 'incremental' ``` Maak een map `application/migrations/` en voeg migratiebestanden toe met de juiste naamgeving. 4. **CLI-commando uitvoeren** Vanaf de commandoregel kun je nu de commando’s aanroepen: ```bash php index.php cli/tasks migrate php index.php cli/tasks clear_cache ``` 5. **Extra tips** - Zorg dat je `index.php` en je server zo zijn ingesteld dat CLI-toegang is toegestaan. - Je kunt ook extra functies toevoegen voor batchtaken, bijvoorbeeld door parameters te gebruiken. 6. **Veiligheid en best practices** - Controleer of het script alleen via CLI wordt uitgevoerd door te controleren op `PHP_SAPI`: ```php if (PHP_SAPI !== 'cli') { exit('Dit script kan alleen via CLI worden uitgevoerd.'); } ``` - Voeg eventueel authenticatie of checks toe om onbedoeld gebruik te voorkomen. --- **Samenvatting:** - Maak een controller in `application/controllers/cli/`. - Implementeer methodes voor migraties en cache legen. - Roep deze methodes via de CLI aan met `php index.php cli/tasks [methode]`. - Configureer migraties en cache correct. Wil je dat ik voorbeeldmigraties of meer geavanceerde batchprocessen toevoeg?
provider: deepseek
model: deepseek-chat
Ik help je graag met het ontwerpen en implementeren van aangepaste CLI-commando's in CodeIgniter 3.1.11. Hier is de complete aanpak: ## 1. CLI Configuratie Maak eerst een CLI-configuratiebestand aan in `application/config/cli.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); $config['cli_enabled'] = TRUE; $config['cli_controllers'] = [ 'Migrate', 'Cache', 'Batch' ]; ``` ## 2. CLI Controller Basisklasse Maak een basis CLI controller in `application/core/MY_CLI_Controller.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class MY_CLI_Controller extends CI_Controller { public function __construct() { parent::__construct(); // Alleen toegestaan via CLI if (!$this->input->is_cli_request()) { show_error('Deze controller is alleen toegankelijk via de command line.', 403); } $this->load->library('migration'); $this->load->driver('cache'); } protected function output($message, $type = 'info') { $colors = [ 'info' => "\033[0;36m", // Cyan 'success' => "\033[0;32m", // Green 'warning' => "\033[0;33m", // Yellow 'error' => "\033[0;31m", // Red 'reset' => "\033[0m" // Reset ]; echo $colors[$type] . $message . $colors['reset'] . PHP_EOL; } } ``` ## 3. Migratie Controller Maak `application/controllers/Migrate.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Migrate extends MY_CLI_Controller { public function index() { $this->output("CodeIgniter Database Migratie Tool", 'info'); $this->output("Gebruik: php index.php migrate [actie]", 'info'); $this->output("Beschikbare acties: latest, version [num], rollback, reset", 'info'); } public function latest() { $this->output("Uitvoeren van migratie naar laatste versie...", 'info'); if ($this->migration->latest()) { $this->output("Migratie succesvol uitgevoerd!", 'success'); } else { $this->output("Fout bij migratie: " . $this->migration->error_string(), 'error'); } } public function version($version = null) { if ($version === null) { $this->output("Versie parameter vereist!", 'error'); return; } $this->output("Uitvoeren van migratie naar versie $version...", 'info'); if ($this->migration->version($version)) { $this->output("Migratie naar versie $version succesvol!", 'success'); } else { $this->output("Fout bij migratie: " . $this->migration->error_string(), 'error'); } } public function rollback() { $this->output("Uitvoeren van rollback...", 'info'); if ($this->migration->version(0)) { $this->output("Rollback succesvol uitgevoerd!", 'success'); } else { $this->output("Fout bij rollback: " . $this->migration->error_string(), 'error'); } } public function reset() { $this->output("Resetten van database...", 'warning'); $this->output("Deze actie verwijdert alle tabellen!", 'warning'); // Bevestiging vragen echo "Weet je het zeker? (ja/nee): "; $handle = fopen ("php://stdin","r"); $line = fgets($handle); if(trim($line) != 'ja'){ $this->output("Operatie geannuleerd.", 'info'); exit; } fclose($handle); if ($this->migration->version(0)) { $this->output("Database reset succesvol!", 'success'); } else { $this->output("Fout bij reset: " . $this->migration->error_string(), 'error'); } } } ``` ## 4. Cache Controller Maak `application/controllers/Cache.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Cache extends MY_CLI_Controller { public function index() { $this->output("CodeIgniter Cache Beheer Tool", 'info'); $this->output("Gebruik: php index.php cache [actie]", 'info'); $this->output("Beschikbare acties: clear, clear_all, status", 'info'); } public function clear($type = null) { if ($type === null) { $this->output("Cache type vereist! (bijv. page, db, opcache)", 'error'); return; } $this->output("Legen van $type cache...", 'info'); if ($this->cache->$type->clean()) { $this->output("$type cache succesvol geleegd!", 'success'); } else { $this->output("Fout bij legen van $type cache", 'error'); } } public function clear_all() { $this->output("Legen van alle cache types...", 'info'); $drivers = ['file', 'apc', 'memcached', 'redis', 'wincache']; $success = true; foreach ($drivers as $driver) { if ($this->cache->$driver->is_supported()) { $this->output("Legen van $driver cache...", 'info'); if (!$this->cache->$driver->clean()) { $success = false; $this->output("Fout bij legen van $driver cache", 'error'); } } } if ($success) { $this->output("Alle cache succesvol geleegd!", 'success'); } } public function status() { $this->output("Cache Status Overzicht:", 'info'); $drivers = ['file', 'apc', 'memcached', 'redis', 'wincache']; foreach ($drivers as $driver) { $status = $this->cache->$driver->is_supported() ? '✓ Ondersteund' : '✗ Niet ondersteund'; $this->output("$driver: $status", 'info'); } } } ``` ## 5. Batch Controller (Optioneel) Maak `application/controllers/Batch.php` voor batchprocessen: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Batch extends MY_CLI_Controller { public function index() { $this->output("Batch Processing Tool", 'info'); $this->output("Beschikbare commando's:", 'info'); $this->output(" php index.php batch process_users", 'info'); $this->output(" php index.php batch cleanup_logs", 'info'); } public function process_users() { $this->output("Start verwerken van gebruikers...", 'info'); // Voorbeeld batch proces $this->load->model('user_model'); $users = $this->user_model->get_users_to_process(); $this->output("Aantal te verwerken gebruikers: " . count($users), 'info'); foreach ($users as $user) { // Verwerk logica hier $this->output("Verwerken gebruiker: " . $user->email, 'info'); } $this->output("Gebruikersverwerking voltooid!", 'success'); } public function cleanup_logs($days = 30) { $this->output("Opruimen van logs ouder dan $days dagen...", 'info'); $this->load->model('log_model'); $deleted = $this->log_model->delete_old_logs($days); $this->output("$deleted logbestanden verwijderd", 'success'); } } ``` ## 6. Gebruiksvoorbeelden ```bash # Migratie commando's php index.php migrate latest php index.php migrate version 5 php index.php migrate rollback php index.php migrate reset # Cache commando's php index.php cache clear page php index.php cache clear_all php index.php cache status # Batch commando's php index.php batch process_users php index.php batch cleanup_logs 90 ``` ## 7. Aanvullende Configuratie Zorg ervoor dat je migratieconfiguratie correct is ingesteld in `application/config/migration.php`: ```php $config['migration_enabled'] = TRUE; $config['migration_path'] = APPPATH . 'migrations/'; $config['migration_version'] = 20240101; $config['migration_table'] = 'migrations'; ``` ## 8. Automatisering met Cron Jobs Voor automatische uitvoering kun je cron jobs instellen: ```bash # Elke dag om 2:00 cache legen 0 2 * * * php /pad/naar/je/project/index.php cache clear_all # Elke nacht migraties uitvoeren 0 3 * * * php /pad/naar/je/project/index.php migrate latest ``` Deze implementatie biedt een robuuste basis voor CLI-automatisering in CodeIgniter 3.1.11 met duidelijke foutafhandeling en gebruiksvriendelijke output.