Welcome Guest, Not a member yet? Register   Sign In
How to make and include different routing files inside one route file?
#5

I use autoload config for each module loadup and automatic routes

You must have Config/Routes.php and Config/Services.php for each module

Modules structure: attached image

app/Config/Autoload.php file: this does load every module and their services and routes automatically

<?php

namespace Config;

use CodeIgniter\Config\AutoloadConfig;

/**
* -------------------------------------------------------------------
* AUTO-LOADER
* -------------------------------------------------------------------
*
* This file defines the namespaces and class maps so the Autoloader
* can find the files as needed.
*
* NOTE: If you use an identical key in $psr4 or $classmap, then
* the values in this file will overwrite the framework's values.
*/
class Autoload extends AutoloadConfig
{
    /**
    * -------------------------------------------------------------------
    * Namespaces
    * -------------------------------------------------------------------
    * This maps the locations of any namespaces in your application to
    * their location on the file system. These are used by the autoloader
    * to locate files the first time they have been instantiated.
    *
    * The '/app' and '/system' directories are already mapped for you.
    * you may change the name of the 'App' namespace if you wish,
    * but this should be done prior to creating any namespaced classes,
    * else you will need to modify all of those classes for this to work.
    *
    * Prototype:
    *
    *  $psr4 = [
    *      'CodeIgniter' => SYSTEMPATH,
    *      'App'        => APPPATH
    *  ];
    *
    * @var array<string, string>
    */
    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
    ];

    /**
    * -------------------------------------------------------------------
    * Class Map
    * -------------------------------------------------------------------
    * The class map provides a map of class names and their exact
    * location on the drive. Classes loaded in this manner will have
    * slightly faster performance because they will not have to be
    * searched for within one or more directories as they would if they
    * were being autoloaded through a namespace.
    *
    * Prototype:
    *
    *  $classmap = [
    *      'MyClass'  => '/path/to/class/file.php'
    *  ];
    *
    * @var array<string, string>
    */
    public $classmap = [];

    /**
    * Collects the application-specific autoload settings and merges
    * them with the framework's required settings.
    *
    * NOTE: If you use an identical key in $psr4 or $classmap, then
    * the values in this file will overwrite the framework's values.
    */
    public function __construct()
    {
        parent::__construct();

        $this->includeModules();
    }

    protected function includeModules()
    {
        $modules = array_filter(glob('../Modules/*/*'), 'is_dir');

        if (!empty($modules)) {
            foreach ($modules as $key => $module_path) {
                if (strpos($module_path, '/') !== false) {
                    $split_path = explode('/', $module_path);
                    if (array_key_exists(2, $split_path) && array_key_exists(3, $split_path)) {
                        $this->psr4['Modules'.'\\'.$split_path[2].'\\'.$split_path[3]] = ROOTPATH.'Modules/'.$split_path[2].'/'.$split_path[3];
                    }
                }
            }
        }
    }

}


Attached Files Thumbnail(s)
   
Reply


Messages In This Thread
RE: How to make and include different routing files inside one route file? - by davis.lasis - 04-18-2021, 09:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB