Welcome Guest, Not a member yet? Register   Sign In
Is it possible to have a Pager config file in a module's config directory?
#1

I'm working on a blog module for CI4 and I want to customize the pagination links. 

In the doc it says the pager view can be overridden in app/Config/Pager.php, and in module's doc it says you can have a Config directory for your module, but there's not much examples other than how to load a custom config file.

Can I have a Pager config file in my module? Something like my-module/Config/Pager.php? I can't make it work. Maybe I need to override the pager service to load my module's config?
Reply
#2

I'm back with this problem...

I just reinstalled a new test site with CI 4.0.2 and I'm still stuck with this problem. For my app to use the custom pagination template from my module, I need to edit app/Config/Pager.php because CI won't load my-module/Config/Pager.php.

In the debug toolbar, I can see that only APPPATH/Config/Pager.php is loaded. Is this a bug? Did we forget to check if there's a Pager.php file in a module's config directory?
Reply
#3
Bug 
(This post was last modified: 01-20-2025, 01:36 PM by mavelo.)

Encountering same issue in 4.6.0 with automatic pagination using CodeIgniter's model.

Config/Autoload
PHP Code:
public $psr4 = [
    APP_NAMESPACE => APPPATH,
    'Admin' => ROOTPATH 'admin',
]; 

App\Model\UserModel
PHP Code:
class UserModel extends Model {
    protected $table 'user';
    protected $primaryKey 'user_id';
    protected $returnType 'object';
    protected $allowedFields = ['user_group_id''username''email''password''firstname''lastname''image''status'];


App\Config\Pager
PHP Code:
public array $templates = [
    'default_full'  => 'CodeIgniter\Pager\Views\default_full',
    'default_simple' => 'CodeIgniter\Pager\Views\default_simple',
    'default_head'  => 'CodeIgniter\Pager\Views\default_head',
]; 

Admin\Config\Pager
PHP Code:
public array $templates = [
    'default_full'  => 'Admin\Views\common\pager_full',
    'default_simple' => 'Admin\Views\common\pager_simple',
]; 

Admin\Controllers\User
PHP Code:
$model model('UserModel');
$data = [
    'rows' => $model->paginate(),
    'pager' => $model->pager->links(),
]; 

App\Config\Pager views are loaded instead of those defined in Admin\Config\Pager

All module views load fine except for pagination.

[Image: vxNUeT3.png]

Loading the module config before the model gets around the issue...

PHP Code:
config('Admin\Pager'); 

But shouldn't Admin\Config\Pager be loaded automatically?
Reply
#4

(This post was last modified: 01-20-2025, 01:58 PM by davis.lasis.)

https://github.com/codeigniter4/CodeIgni...es.php#L67


PHP Code:
    /**
    * --------------------------------------------------------------------------
    * Auto-Discovery Rules
    * --------------------------------------------------------------------------
    *
    * Aliases list of all discovery classes that will be active and used during
    * the current application request.
    *
    * If it is not listed, only the base application elements will be used.
    *
    * @var list<string>
    */
    public $aliases = [
        'events',
        'filters',
        'registrars',
        'routes',
        'services',
    ]; 


Module configs are not autoloaded. Registrars are, so you can make registrar in that module and your config should be overrriden.
With Pager config I have not tested this though

Example to override App supportedLocales, maybe you can use this approach

PHP Code:
<?php namespace Modules\Language\Config;

class 
Registrar
{
    public static function App(): array
    {
        $output = [];

        if (isset($_SERVER['REQUEST_URI'])) {
            $db db_connect();

            if ($db->tableExists('languages')) {
                $results $db->table('languages')->where('published'1)->get()->getResultObject();

                foreach ($results as $result) {
                    if (!isset($output['supportedLocales'])) {
                        $output['supportedLocales'] = [];
                    }

                    if (!in_array($result->language$output['supportedLocales'])) {
                        $output['supportedLocales'][] = $result->language;
                    }

                    if (!empty($result->is_default)) {
                        $output['defaultLocale'] = $result->language;
                    }
                }
            }
        }

        return $output;
    }

Reply
#5
Lightbulb 
(This post was last modified: 01-20-2025, 02:43 PM by mavelo.)

@includebeer How I got around the problem below. Replace "Admin" with your module's namespace.

\Admin\Controllers\BaseController
PHP Code:
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger) {
    parent::initController($request$response$logger);
    config('Admin\Pager');


Could not determine why module Pager config is not automatically loaded Confused

Quote:Module configs are not autoloaded.

admin/Config/Routes.php gets autoloaded in my module Huh
Reply
#6

Well... $aliases list is config classes which are autoloaded

PHP Code:
public $aliases = [
        'events',
        'filters',
        'registrars',
        'routes',
        'services',
    ]; 


Actually in that case, just add Pager to this list
Reply
#7

(This post was last modified: 01-20-2025, 04:12 PM by mavelo.)

Quote:Actually in that case, just add Pager to this list

No obvious effect. Still had to call config('Admin\Pager') before model to make module templates load.

Just now realizing admin/Config/App.php is not loaded either (eg: base_url() returns app base, not module base). No point beating that horse  Dodgy

We thought modularization was a slim alternative to multi-app single install.
  • Latter seems like overkill for separating widely-related front/back ends.
  • While the prior is missing built-in module-specific config autoloading.
Maybe we'll modify the core to achieve the latter. Toss out a pull-request for the powers-that-be if it seems like a community fit  Tongue

Opinion: Autoloading modular config for respective classes by default should be an option.
Reply
#8

@davis.lasis thanks for referencing registrars. After a fresh night's sleep, that is superior to our previous workarounds.

PHP Code:
<?php

namespace Admin\Config;

class 
Registrar
{
    public static function Pager(): array
    {
        return [
            'templates' => [
 
                'default_full'  => 'Admin\Views\common\pager_full',
 
                'default_simple' => 'Admin\Views\common\pager_simple',
            ],
            'perPage' => 10,
        ];
    }


While digging around CodeIgniter\Config\Factories::getOptions() we spotted "registrar" in code comments, and recalled your mention thereof, sending us down the path.

@includebeer This Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB