Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Framework Laravel Package

codeigniter/framework

CodeIgniter is a lightweight PHP web framework focused on speed, simplicity, and small footprint. It offers MVC structure, clean routing, security features, database tools, and helpers—ideal for building fast, maintainable apps without heavy dependencies.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. Installation

    • Use Composer to install via:
      composer create-project codeigniter4/appstarter my-project
      
    • Or add to an existing project:
      composer require codeigniter4/framework
      
  2. Project Structure

    • Key directories:
      • app/ – Core application logic (Models, Controllers, Views, Libraries).
      • public/ – Web root (entry point: index.php).
      • writable/ – Configs, caches, logs (ensure permissions: chmod -R 775 writable).
  3. First Use Case: Hello World

    • Create a controller:
      // app/Controllers/Home.php
      namespace App\Controllers;
      class Home extends BaseController {
          public function index() {
              return view('welcome_message');
          }
      }
      
    • Create a view:
      // app/Views/welcome_message.php
      <h1>Hello, CodeIgniter!</h1>
      
    • Run the server:
      php spark serve
      
    • Visit http://localhost:8080.
  4. Configuration

    • Edit app/Config/ files (e.g., App.php, Database.php).
    • Environment variables: .env (copy .env.example first).
  5. Routing

    • Define routes in app/Config/Routes.php:
      $routes->get('/', 'Home::index');
      

Implementation Patterns

Core Workflows

MVC Integration

  • Controllers: Extend BaseController; use dependency injection (DI) for services.
    class UserController extends BaseController {
        public function __construct(private UserModel $userModel) {}
    }
    
  • Models: Extend BaseModel for database interactions.
    class UserModel extends BaseModel {
        protected $table = 'users';
        protected $allowedFields = ['name', 'email'];
    }
    
  • Views: Use view() helper or BaseController's render() method.
    return view('user/profile', ['user' => $user]);
    

Dependency Injection (DI)

  • Register services in app/Config/Services.php:
    services => [
        'auth' => \App\Services\AuthService::class,
    ],
    
  • Inject into controllers:
    public function __construct(private AuthService $auth) {}
    

Database Operations

  • Query Builder:
    $users = $db->table('users')->where('active', 1)->get();
    
  • Migrations:
    php spark migrate
    
    Define migrations in app/Database/Migrations/.

Validation

  • Use Validation library:
    $rules = [
        'email' => 'required|valid_email',
        'password' => 'required|min_length[8]',
    ];
    $validation = \Config\Services::validation();
    if (!$validation->run($rules, $data)) {
        throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
    }
    

API Development

  • Use JSONResponse for APIs:
    return $this->response->setJSON([
        'status' => 'success',
        'data' => $users,
    ]);
    
  • Leverage Filter for API input sanitization.

Caching

  • Enable caching in app/Config/Cache.php.
  • Use Cache service:
    $cache = \Config\Services::cache();
    $data = $cache->get('key');
    $cache->save('key', $data, 60);
    

Events and Filters

  • Events: Dispatch and listen in app/Config/Events.php.
    Events::on('preSystemShutdown', function () {
        log_message('info', 'System shutting down...');
    });
    
  • Filters: Use before/after hooks in controllers or globally in app/Config/Filters.php.

Integration Tips

Third-Party Libraries

  • Load libraries in app/Config/Services.php:
    'library' => [
        'parser' => \CodeIgniter\Parser\Parser::class,
        'session' => \CodeIgniter\Session\Session::class,
    ],
    
  • Use Services facade:
    $session = service('session');
    

CLI Tasks

  • Create custom CLI commands by extending BaseCommand:
    // app/Commands/Example.php
    namespace App\Commands;
    use CodeIgniter\CLI\BaseCommand;
    
    class Example extends BaseCommand {
        public $group = 'custom';
        public $name = 'example';
        protected $description = 'Example command';
    
        public function run() {
            $this->output->writeln('Hello from CLI!');
        }
    }
    
  • Register in app/Config/Commands.php and run:
    php spark example
    

Testing

  • Use phpunit (included):
    composer test
    
  • Write tests in tests/ directory:
    // tests/Unit/HomeTest.php
    public function testIndex() {
        $response = $this->get('/');
        $response->assertOK();
    }
    

Security

  • Use Security helper for XSS/CSRF protection:
    echo esc($userInput); // Escape output
    csrf_token(); // In forms
    
  • Configure in app/Config/Security.php.

Gotchas and Tips

Pitfalls

  1. Case Sensitivity

    • File/namespace paths are case-sensitive on Linux/macOS. Stick to lowercase for consistency.
  2. Autoloading Issues

    • Ensure composer dump-autoload runs after adding new classes.
    • Verify autoload.psr-4 in composer.json includes app/.
  3. Database Transactions

    • Use db->transStart()/transComplete() for multi-query transactions:
      $db->transStart();
      try {
          $db->table('users')->insert($data);
          $db->table('logs')->insert(['user_id' => $userId]);
          $db->transComplete();
      } catch (\Exception $e) {
          $db->transRollback();
      }
      
  4. Session Handling

    • Sessions require session driver in app/Config/Sessions.php.
    • Ensure writable/sessions is writable (chmod 775).
  5. Route Caching

    • Cache routes for performance but clear cache after changes:
      php spark route:clear
      
  6. Model Relationships

    • Use hasMany, belongsTo, etc., but ensure foreign keys match:
      protected $hasMany = ['posts' => PostModel::class];
      
  7. File Uploads

    • Validate file types/sizes in app/Config/Validation.php:
      'upload' => 'uploaded[file]|max_size[file,1024]|mime_in[file,image/jpg,image/png]',
      
  8. Environment Switching

    • Use .env for environment-specific configs (e.g., APP_ENV=development).
    • Avoid hardcoding paths; use rootPath(), baseURL().

Debugging Tips

  1. Error Logging

    • Check writable/logs/ for errors.
    • Enable debug mode in app/Config/App.php:
      debugToolbar => env('CI_DEBUG_TOOLBAR', true),
      
  2. Debug Toolbar

    • Install codeigniter4-debugbar for a PHP Debug Bar:
      composer require --dev peridot-php/codeigniter4-debugbar
      
  3. Var Dumping

    • Use var_dump() or dd() (from Debug helper):
      dd($variable); // Dump and die
      
  4. Query Logging

    • Enable in app/Config/Database.php:
      'debug' => env('CI_DEBUG_DATABASE', true),
      
  5. Route Debugging

    • List all routes:
      php spark route:list
      

Extension Points

  1. Custom Libraries

    • Place in app/Libraries/ and autoload in composer.json:
      "autoload": {
          "psr-4": {
              "App\\": "app/"
          }
      }
      
  2. Custom Helpers

    • Add to app/Helpers/ and register in app/Config/Autoloader.php:
      $psr4['App\\Helpers'] = APPPATH . 'Helpers';
      
  3. Custom Commands

    • Extend BaseCommand (see CLI Tasks above).
  4. Custom Filters

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views