router icon indicating copy to clipboard operation
router copied to clipboard

how to separating routes definition - HMVC

Open ahmaruff opened this issue 2 years ago • 0 comments

i tried using HMVC design pattern. my idea is, creating Routes.php for each modules, and then include them in main Routes.php
my project structure

src/
    |-Config/Config.php
    |-Core/
    |     |-CoreFunctions.php
    |     |-Routes.php (main routes)
    |-Modules/
    |     |-Home/
    |          |-HomeController.php
    |          |-Routes.php (modules routes definition) 
    |-Views/

Config.php

class Config {
    public $app_path;
    public function __construct() {
        $this->app_path = dirname(__DIR__);
    }
}

Core/Routes.php

$configs = new Config();

$router = new \Bramus\Router\Router();

$router->set404(function(){
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo "404 - not found";
});

$router->get('/',function(){
    echo "hello world";
});

$router->mount('/home',function(){
    global $configs;
    $item_dir = $configs->app_path.'Modules/Home';
    if(file_exists("{$item_dir}/Routes.php")){
        include_once("{$item_dir}/Routes.php");
    }
});

$router->run();

Modules/Home/Routes.php

<?php

$router->get('/p', function(){
    echo "PPP";
});

localhost:8080/ print : hello world (success)

localhost:8080/home print : 404 - not found

localhost:8080/home/p print : 404 - not found


my question is, how can I achive this?,

ahmaruff avatar Aug 14 '22 18:08 ahmaruff