Same Routeprefix for multiple Modules

Hello,

i have splitted a big module into 3 separate modules.(not finished)

It’s an ISP tool, with which one you could manage customer and contracts, invoice and much more…
I have created composer packages from them

Current Modules:

isptool-customer
isptool-contract
isptool-invoice

isptool-contract/Modules.php:

<?php

namespace xxx\isptool\contract;

use yii\base\Module as BaseModule;
use Yii;

class Module extends BaseModule
{
    const VERSION = '0.1';

    /** @var array Model map */
    public $modelMap = [];

	public $urlPrefix = '';

    public $dbConnection = 'db';

    /** @var array The rules to be used in URL management. */
	public $urlRules = [
		
	];

	public function init(){
		parent::init();

		$this->params = require __DIR__ . '/config/params.php';
	}


    /**
     * @return string
     */
    public function getDb()
    {
        return \Yii::$app->get($this->dbConnection);
    }
}

isptool-contract/Bootstrap.php:

<?php

namespace xxx\isptool\contract;

use Yii;
use yii\base\BootstrapInterface;
use yii\console\Application as ConsoleApplication;
use yii\i18n\PhpMessageSource;
use yii\web\GroupUrlRule;

class Bootstrap implements BootstrapInterface
{
    public function bootstrap($app)
	{
	
		if ($app->hasModule('isptool-contract') && ($module = $app->getModule('isptool-contract')) instanceof Module) {
			if ($app instanceof ConsoleApplication) { 
			}else{
				
				$configUrlRule = [
					'prefix' => 'isptool',
					'rules' => $module->urlRules,
				];


				if ($module->urlPrefix != 'isptool') {
					$configUrlRule['routePrefix'] = '';
				}
				$rules = new GroupUrlRule($configUrlRule);
				$app->urlManager->addRules([$rules],false);
				
			}
		}
    }
}

Everything works fine, but i don’t like the Urlrules.

Current rules looks like isptool-customer/customer/create i.e

But i want something like

isptool/customer/create

or better

customer/create

Is this possible?

1 Like

Implement custom URL Rules for URL Manager config

'rules' => [
    'customer/<action>' => 'isptool/<controller>/<action>',
]

Note I have not tested it and it’s long since I dealt with that one but with little effort it should work!

1 Like

thanks @evstevemd .

It was my fault in the extension with “urlPrefix” and groupPrefix.
Here is my whole solution, for other, maybe with same kind of problem :slight_smile:

Module.php

public $urlPrefix = 'customer';
public $urlRules = [
        '/' => 'customer/index',
        '<id\d+>' => 'customer/view',
        'customer/<action>' => '<controller>/<action>',
];

Bootstrap.php

    public function initRoutes(Webapplication $app){
            $module = $app->getModule('isptool-customer');
            $configUrlRule = [
                    'class' => 'yii\web\GroupUrlRule',
                    'prefix' => $module->urlPrefix,
                    'rules' => $module->urlRules,
            ];


            if ($module->urlPrefix != 'isptool-customer') {
                    $configUrlRule['routePrefix'] = 'isptool-customer';
            }
            $rules = Yii::createObject($configUrlRule);
            $app->urlManager->addRules([$rules],false);

    }
2 Likes