php - Namespace broken on moving module

195

I wrote a module in the backend with this name space:

namespace backend\modules\payment;

so if I move module to another project in frontend the module will broke;

and another problem is that I add module with name "payment" in config

'payment' => 
[
            'class' => 'backend\modules\payment\Bank',
            'components' => 
            [
                'service'  => 
                [
                    'class' => 'backend\modules\payment\components\Service',
                ]
            ]
]

and I get full url to actionReturn in module's "service" component using this method:

public function getReturnUrl()
{
  return \yii\helpers\Url::toRoute('payment/return',true);
}

now if I want to change module name in another project , I have to go and change all this functions to get valid url , is there any way to fix this and action's url not be depended on module name

491

Answer

Solution:

If you create the module in your vendor namespace you ca refer to the module always with the same url / routing

and you can easy install the module copyng tour vendor subdir

 vendor\yourVendorName\yourModuleName\  

configure properly themodule.php and add in modules section in main.php

If you think the module you assign inconfig/main.php could conflit with other module you shoudl assign to your module a proper name.

The name use in url for module is the name you assign in config/main.php section modules

this way:

      'modules' => [
          .....
          'auth'=> [    
              'class' => 'vendor\dfenx\auth\Module',
          ],
          'yourModuleName' => [ 
              'class' => 'vendor\yourVendorName\yourModuleDir\Module',
          ],
          'anotherModuleName' =>[ 
              'class' => 'vendor\anotherVendorName\anotherModuleDir\Module',
          ],

         .....

       ],

obviously the modules name yourModuleName and anotherModuleName can't be the same otherwise the ruoting based on the url can't work properly.

In last you could assigne the name of your module in the url to a vars and mapping to this var the module name you assign in the config. In this way the name of module and the related name in url are completed easily remappable.

I hope this is useful for you

People are also looking for solutions to the problem: php - Laravel redirect with javascript

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.