Class 'CWebModule' not found when adding Yii2 Quiz - php

I have added yii2 quiz module in my project
I have added the module inside the frontend/modules/quiz
Then in the app's main.php i have added something like
'modules' => [
'quiz' => [
'class' => 'app\modules\quiz\Module',
]
],
Then after running it, I'm getting the "CWebModule not found".
class Module extends CWebModule{
......
}
My module class extending CWebModule.
How to fix this error?

Related

Geolocation module - how to call abstract class method properly

I'm using the custom module in my project, and it was working fine with Geolocation 1.11 module.
After I've updated Geolocation to 3.7 my custom module stopped working.
What I've found - my custom module was using trait from 'GoogleMapsDisplayTrait.php' and now it is missing in Geolocation. It was like that
class LocationsMapBlock extends BlockBase {
use GoogleMapsDisplayTrait;
public function build() {
return [
'#theme' => 'locations_map',
'#attached' => [
'library' => ['location/map'],
'drupalSettings' => [
'geolocation' => [
'google_map_url' => $this->getGoogleMapsApiUrl(),
],
'locations' => $this->getLocationMapData(),
],
],
];
}
}
I've found that now most similar class to trait GoogleMapsDisplayTrait) is inside geolocation_google_maps submodule of geolocation module.
But now it's not a trait, but abstract class with the same methods as previous trait was.
abstract class GoogleMapsProviderBase extends MapProviderBase {
...
}
I've tried to add this class:
use Drupal\geolocation_google_maps\GoogleMapsProviderBase;
But now I'm receiving the error
'Error: Call to undefined method Drupal\location\Plugin\Block\LocationsMapBlock::getGoogleMapsApiUrl() in Drupal\location\Plugin\Block\LocationsMapBlock->build() (line 32 of modules/custom/location/src/Plugin/Block/LocationsMapBlock.php).'
My PHP knowledges is to weak to find the solution...
The undefined function getGoogleMapsApiUrl() is, maybe the file is missing?
Check if the file exists:
/modules/geolocation/modules/geolocation_google_maps/src/GoogleMapsProviderBase.php
Try to install and enable the following modules for a cleaner working with geolocation - specially for the geolocation_google_maps:
geofield
geolocation_geofield
geolocation_google_maps
Try using this call instead of $this->getGoogleMapsApiUrl():
Drupal::service('plugin.manager.geolocation.mapprovider')->getMapProvider('google_maps')->getGoogleMapsApiUrl()

Same name can use for controller outside module which have same module name

I have a controller called leave which is outside of the module and have a module name called leave. When i access a action insdie the leave controller then it goes to leave module. So how i can access the leave controller.
Below is the module initialization.
'modules' => [
'leave' => [
'class' => 'app\modules\leave\Module',
],
....
]
I have a leaveController outside the module. Can anyone help regarding this.

Yii2 load modules

how I can load an external-site module? I have a common module I need to load in distinct Yii2 sites, like advanced-template my idea is to have a common dir where store generic modules wich I can load to each site. A file system structure can be like this:
/
site-1/
(loads modules from common-modules dir for site-1)
site-2/
(loads modules from common-modules dir for site-2)
common_sites_modules/
module-1/
module-2/
carrello/
Carrello.php
Each site in his configuration have to load modules from common-modules/
Is possible to implement this structure?
Edit 1
The configuration:
'cart' => [
'class' => dirname(dirname(dirname(__DIR__))) . '/common_sites_modules/carrello/Carrello',
'params' =>[
...
],
'components' => [
...
],
],
and this is the first line of the class Carrello.php:
<?php
namespace common_sites_modules\carrello;
...
The top bar of editor with the path of the class and the error returned by Yii:
Edit 2:
Thanks to #Yupik for support and suggests, the new settings:
bootstrap.php:
Yii::setAlias('#common-modules', dirname(dirname(dirname(__DIR__))) . '/common_sites_modules');
main-local.php:
'class' => '#common-modules\carrello\Carrello',
The generated error:
Like suggested in the comments the solution is to declare an alias and then use the name of alias for call the module. Like suggested by #Yupik I've set in the common/config/bootstrap.php an alias as follow:
Yii::setAlias('#common_modules', dirname(dirname(dirname(__DIR__))) . '/common_modules');
In the main configuration:
'carrello' => [
'class' => ''common_modules\carrello\Carrello',
...
]
Obviously namespace have to be configured based on the position on filesystem.
Thanks for the suggestions
Yes, we can do this by making a symlink of common_sites_modules directory in both site folder (site1, site2).

ZF3 Navigation "plugin" error

Following https://docs.zendframework.com/zend-navigation/quick-start/, i try to make a navigation for my application. I registered a navigation, i added the DefaultNavigationFactory to the service-manager, but i get an error when i try to print the navigation.
This is my module/Application/config/module.config.php:
namespace Application;
use Zend\Navigation\Service\DefaultNavigationFactory;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\View\Helper\Navigation;
return [
'navigation' => [
'default' => [
/* ... */
]
] ,
'service_manager' => [
'factories' => [
'navigation' => DefaultNavigationFactory::class,
],
],
];
But when $this->navigation('default')->menu(), i get this error, excluding the stack trace:
Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by the name "navigation" was not found in the plugin manager Zend\View\HelperPluginManager in C:\Users\bikke_000\Documents\Sites\slapenenzo\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php:133
Running
composer require zendframework/zend-navigation
fixed the issue it seems that in zf3 navigation isn't installed by default
In addition to the answer of #Ponsjuh I had to include the module "Zend\Navigation" in my modules config.
you need to pass 'navigation' instead of 'default' as below:
$this->navigation('navigation')->menu();
Check your "development mode". In production mode, 'config_cache_enabled' is true. When you install new module, you must refresh cache.
Default: 'data/cache/module-config-cache.application.config.cache.php'

Namespace broken on moving module

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
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 the module.php and add in modules section in main.php
If you think the module you assign in config/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

Categories