• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make and include different routing files inside one route file?

#1
I am trying to make more robust app with CI4. I am trying to separate many functions along with their routing configs.
But I can't find a way to include another routing file inside Routes.php

Here is an example:

There is a route file called example.route inside /Views
 How can I include that file inside Routes.php so that the Routs.php looks cleaner .
Reply

#2
Something like this for modules.

PHP Code:
app/Config/Routes.php 
//.. Other routes

// Add this to Footer 

// Including all module routes

$modules_path ROOTPATH 'Modules/';
$modules      scandir($modules_path);

foreach (
$modules as $module)
{
    if ($module === '.' || $module === '..')
    {
        continue;
    }

    if (is_dir($modules_path) . '/' $module)
    {
        $routes_path $modules_path $module '/Config/Routes.php';
    
        
if (file_exists($routes_path))
        {
            require $routes_path
        }
        else
        {
            continue;
        }
    }


Should get you going.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(04-17-2021, 12:41 PM)InsiteFX Wrote: Something like this for modules.

PHP Code:
app/Config/Routes.php 
//.. Other routes

// Add this to Footer 

// Including all module routes

$modules_path ROOTPATH 'Modules/';
$modules      scandir($modules_path);

foreach (
$modules as $module)
{
    if ($module === '.' || $module === '..')
    {
        continue;
    }

    if (is_dir($modules_path) . '/' $module)
    {
        $routes_path $modules_path $module '/Config/Routes.php';
    
        
if (file_exists($routes_path))
        {
            require $routes_path
        }
        else
        {
            continue;
        }
    }


Should get you going.
Would you please explain this code to me ?
Where should I put this code ?
Reply

#4
Right where the first line says to put it app/Config/Routes.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.