Welcome Guest, Not a member yet? Register   Sign In
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

(This post was last modified: 04-18-2021, 01:46 AM by InsiteFX.)

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
#6

(This post was last modified: 04-25-2023, 09:48 AM by Corsari.)

Hello
this demo
https://github.com/michalsn/codeigniter-htmx-demo
displaces an additional Routes.php file in a separate path and it works
in particular it goes in a subfolder where the whole demo is encapsulated

Code:
/App/ThirdParty/htmx-demo/src/Config/Routes.php

though I have not enough knowledge to explain why and how it works
As stated in the above github page, the unique modification done is in the /App/Config/Autoload.php where in the $psr4 it is added the respective namespace

Code:
public $psr4 = [
    APP_NAMESPACE => APPPATH, // For custom app namespace
    'Config'      => APPPATH . 'Config',
    'Michalsn\CodeIgniterHtmxDemo' => APPPATH . 'ThirdParty/htmx-demo/src',
];

what is fantastic for my really limited knowledge of CodeIgniter , is that the default installation of CodeIgniter remains untouched and the parallel subtree unzipped in /App/ThirdParty works

About the additional Routes.php , it works because of the Auto-Discovery https://www.codeigniter.com/user_guide/g...-discovery
Reply
#7

If you just want to include another routes file, just `require` the routes file.
https://www.php.net/manual/en/function.require.php

If you want code modularization, CI4 provides Code Modules:
https://www.codeigniter.com/user_guide/g...dules.html
and a routes file (Module/Config/Routes.php) in a module is automatically discovered by Auto-Discovery
https://www.codeigniter.com/user_guide/g...tml#routes
Reply
#8

Thank you for teaching :-)

I'm trying hard to "move"/"export" that htmx demo files into a standard/default/typical appstarter CodeIgniter setup directories structure

but something is failing :-((
weird because I think to have paid attention in the namespaces rewrite
Reply
#9

(This post was last modified: 04-26-2023, 05:54 AM by luckmoshy.)

This is a much more precise and best-practice approach; otherwise, I do think

PHP Code:
public $psr4 = [
    APP_NAMESPACE => APPPATH// For custom app namespace
    'Config'      => APPPATH 'Config',
    'Namespace\Subnamspace' => APPPATH 'ThirdParty/yourmodule/src',

Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply




Theme © iAndrew 2016 - Forum software by © MyBB