Welcome Guest, Not a member yet? Register   Sign In
Why CI PSR4 do not accept more than one module?
#1

(This post was last modified: 12-22-2023, 08:31 PM by luckmoshy.)

Hi all I was taking a little bit of a look and found that in CI PSR4 autoloader, it does not work for more than two module autoloading as follows:
PHP Code:
public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
          'Config'      => APPPATH 'Config',
//Here if both, only one work why??!!
          'Admin\Editor'  => ROOTPATH 'Admin/Editor',//this is working
          'Admin\User' => ROOTPATH 'Admin/User',//not is not working
    ]; 

Above there are two namespaced modules and all work fine except I have to switch off one
like
PHP Code:
public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
          'Config'      => APPPATH 'Config',
              // 'Admin\Editor' => ROOTPATH . 'Admin/Editor',
 
  'Admin\User' => ROOTPATH 'Admin/User',//This will work because first one is off

    ]; 

and

PHP Code:
public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
          'Config'      => APPPATH 'Config',
          'Admin\Editor' => ROOTPATH 'Admin/Editor',//This will work fine because the second one is off
 // 'Admin\User' => ROOTPATH . 'Admin/User',

    ]; 

Why this or is there any priority like (route priority) should I use?
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#2

(This post was last modified: 12-22-2023, 11:40 PM by kenjis.)

What does not work?
At least the following code works.

PHP Code:
--- /dev/null
+++ b/Foo/Editor/Config/Routes.php
@@ -0,+1,10 @@
+<?
php
+
+namespace 
Foo\Editor\Config;
+
+use 
CodeIgniter\Router\RouteCollection;
+
+
/**
+ * @var RouteCollection $routes
+ */
+$routes->get('editor', [\Foo\Editor\Controllers\Home::class, 'index']);


--- /
dev/null
+++ b/Foo/Editor/Controllers/Home.php
@@ -0,+1,13 @@
+<?
php
+
+namespace 
Foo\Editor\Controllers;
+
+use 
App\Controllers\BaseController;
+
+class 
Home extends BaseController
+{
+
    public function index()
+
    {
+
        return __METHOD__;
+
    }
+}


--- /
dev/null
+++ b/Foo/User/Config/Routes.php
@@ -0,+1,10 @@
+<?
php
+
+namespace 
Foo\User\Config;
+
+use 
CodeIgniter\Router\RouteCollection;
+
+
/**
+ * @var RouteCollection $routes
+ */
+$routes->get('user', [\Foo\User\Controllers\Home::class, 'index']);


--- /
dev/null
+++ b/Foo/User/Controllers/Home.php
@@ -0,+1,13 @@
+<?
php
+
+namespace 
Foo\User\Controllers;
+
+use 
App\Controllers\BaseController;
+
+class 
Home extends BaseController
+{
+
    public function index()
+
    {
+
        return __METHOD__;
+
    }
+}


--- 
a/app/Config/Autoload.php
+++ b/app/Config/Autoload.php
@@ -46,+46,@@ class Autoload extends AutoloadConfig
     
public $psr4 = [
         
APP_NAMESPACE => APPPATH// For custom app namespace
         
'Config'      => APPPATH 'Config',
+        
'Foo\Editor'  => ROOTPATH 'Foo/Editor',
+        
'Foo\User'    => ROOTPATH 'Foo/User',
     ];
 
     
/** 


Code:
$ php spark routes

CodeIgniter v4.4.3 Command Line Tool - Server Time: 2023-12-23 06:38:23 UTC+00:00

+--------+--------+------+-------------------------------------+----------------+---------------+
| Method | Route  | Name | Handler                            | Before Filters | After Filters |
+--------+--------+------+-------------------------------------+----------------+---------------+
| GET    | /      | »    | \App\Controllers\Home::index        |                | toolbar      |
| GET    | editor | »    | \Foo\Editor\Controllers\Home::index |                | toolbar      |
| GET    | user  | »    | \Foo\User\Controllers\Home::index  |                | toolbar      |
+--------+--------+------+-------------------------------------+----------------+---------------+
Reply
#3

(This post was last modified: 12-23-2023, 02:28 AM by luckmoshy.)

Thank @kenjis Every things are now okay but I don't know why if I use

PHP Code:
<?php namespace Foo\User\Config;

$routes->group('user', ['namespace' => 'Foo\User\Controllers'], static function ($routes) {
    
      
//$routes->match(['get', 'post'],'index', 'userHome::index');  does not work
    
    
$routes->get('index', [\Foo\User\Controllers\userHome::class, 'index']); //This style work

 
}); 

I have used your fully qualified root namespace example and now about ten modules work fine !!!!
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#4

It is weird. The following code also works.

PHP Code:
<?php

namespace Foo\User\Config;

use 
CodeIgniter\Router\RouteCollection;

/**
* @var RouteCollection $routes
*/
$routes->group('user', ['namespace' => 'Foo\User\Controllers'], static function ($routes) {
    $routes->match(['get''post'], 'index''Home::index');
}); 

Code:
$ php spark routes

CodeIgniter v4.4.3 Command Line Tool - Server Time: 2023-12-23 09:23:07 UTC+00:00

+--------+------------+------+-------------------------------------+----------------+---------------+
| Method | Route      | Name | Handler                             | Before Filters | After Filters |
+--------+------------+------+-------------------------------------+----------------+---------------+
| GET    | /          | »    | \App\Controllers\Home::index        |                | toolbar       |
| GET    | editor     | »    | \Foo\Editor\Controllers\Home::index |                | toolbar       |
| GET    | user/index | »    | \Foo\User\Controllers\Home::index   |                | toolbar       |
| POST   | user/index | »    | \Foo\User\Controllers\Home::index   |                | toolbar       |
+--------+------------+------+-------------------------------------+----------------+---------------+
Reply




Theme © iAndrew 2016 - Forum software by © MyBB