Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rcorsari opened this issue Feb 26, 2025 · 7 comments
Closed
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@rcorsari
Copy link

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

@rcorsari rcorsari added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 26, 2025
@samsonasik
Copy link
Member

No, you don't rewrite initController() as new generated controller already extends BaseController, use constructor via __construct() instead if you want to do special on object creation initialization.

@neznaika0
Copy link
Contributor

@samsonasik Is there any reason? I'm also extending initController().

@samsonasik
Copy link
Member

initController() is always executed by default, if user want to do special initialization, use __construct instead, make re-init initController is bad dx, for whatever reason.

@rcorsari
Copy link
Author

rcorsari commented Feb 27, 2025

Hello
In that case , in my opinion, the documentation on the guide is confusing

have a look https://codeigniter.com/user_guide/incoming/controllers.html#constructor

In the constructor it doesn't even mention anymore __construct()

It says to use initController()

@samsonasik
Copy link
Member

It mention the __construct, that initController called after it. You can add it, but set it by default is not good, as it it is not required.

@neznaika0
Copy link
Contributor

neznaika0 commented Mar 4, 2025

@samsonasik I decided to move the initialization from initController() to the constructor. But there is a problem - the properties request, response, logger are not initialized. Example set $this->locale = $this->request->getLocale().

@samsonasik
Copy link
Member

samsonasik commented Mar 4, 2025

It depends of what the usage, yes, initController may be needed to be overridden, but that's not always, so make override initController by default is invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
3 participants