Skip to content

Missing Feature: Add initController() by default in php spark make:controller #9465

@rcorsari

Description

@rcorsari

PHP Version

8.2

CodeIgniter4 Version

4.6.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MariaDB 10.8

What happened?

Hello,

I would like to propose an improvement to php spark make:controller.
Currently, when generating a controller using this command, the initController() method is not included by default.

A similar approach is already taken with php spark make:model, where generated models come pre-populated with useful default settings. I believe applying the same logic to controllers would be beneficial.

Reasons for this change:

  • Most controllers end up needing at least one model.
  • The implementation of initController() follows a fairly standard pattern.
  • It is easier to remove unnecessary code than to remember to add important code.

Adding this by default would encourage best practices and improve the developer experience.

Let me know your thoughts!

Best regards,
R.

Steps to Reproduce

php spark make:controller

Expected Output

<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class NomeController extends BaseController
{
// Uncomment and modify as needed
// protected $modelName;
// protected $helpers = ['form', 'url'];

/**
 * Constructor.
 * 
 * NOTE: Use this method only for:
 * - Injecting manually created dependencies
 * - Setting up services that don't depend on framework objects
 * - Configuring third-party libraries
 * 
 * Do NOT use return statements here!
 */
public function __construct()
{
    // Your constructor code here
    // Example: $this->customService = new CustomService();
}

/**
 * Controller initializer.
 * 
 * Use this method for all framework-dependent initialization:
 * - Loading models
 * - Accessing request/response objects
 * - Session handling
 * - Authentication checks
 */
public function initController(
    RequestInterface $request,
    ResponseInterface $response,
    LoggerInterface $logger
) {
    parent::initController($request, $response, $logger);
    
    // Initialize models, libraries, etc. here
    // $this->modelName = new \App\Models\ModelName();
}

public function index()
{
    // Your code here
}

'}`

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions