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).
Related
I'm building ZF3 based application with my own library that contains base components to include in other projects. Library is composer based so the application tree looks like this:
- module
-- Application
--- src
---- view
----- index.phtml (where I want to include partial from base)
- vendor
-- myBaseScripts (installed via composer)
--- partials
---- myTablePartial.phtml (partial to include)
Can I include partial that is not inside of module/Application/view but inside vendor/myBaseScripts/partials?
Sure. However, you're hereby advised to make sure that the module you're creating contains a default view.
To register a view partial, use the config of a module, like so:
'view_manager' => [
'template_map' => [
'name/of/partial/view' => __DIR__ . '/../path/relative/to/config/file/view-partial.phtml',
],
],
Make sure that your module contains something like the above.
You then use this in a view like so:
<?= $this->partial('name/of/partial/view', ['param1' => 'value1']) ?>
$this->partial is a ViewHelper. The first argument it takes is which partial to load (by name!). The second parameter is an optional array, which contains key/value pairs for data.
To overwrite a partial, such as a default module partial, with a custom one in another module, you must overwrite the value of the key/value pair in the config.
This module, with the modified partial, must be loaded after the 'original' one. This is to ensure that the config gets overwritten in the order you're expecting.
Assuming the code above is in your original module, you would do the below in another module:
'view_manager' => [
'template_map' => [
'name/of/partial/view' => __DIR__ . '/../path/relative/to/config/file/this-is-a-custom-partial.phtml',
],
],
As you can see, the name is still the same.
Happy coding.
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
I'm trying to set an alias in Yii2 but I'm getting a Invalid Parameter / Invalid path alias for the below code that is placed in the app config file:
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#webroot/scripts/sceditor/languages/',
],
If I remove the # it works.
I noticed you can do this:
Yii::setAlias('#foobar', '#foo/bar');
...but I would prefer to set it within the app config file. Is this not possible? If so, how?
Yii2 basic application
To set inside config file, write this inside $config array
'aliases' => [
'#name1' => 'path/to/path1',
'#name2' => 'path/to/path2',
],
Ref: http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
But as mentioned here,
The #yii alias is defined when you include the Yii.php file in your entry script. The rest of the aliases are defined in the application constructor when applying the application configuration.
If you need to use predefined alias, write one component and link it in config bootstrap array
namespace app\components;
use Yii;
use yii\base\Component;
class Aliases extends Component
{
public function init()
{
Yii::setAlias('#editor_lang_dir', Yii::getAlias('#webroot').'/scripts/sceditor/languages/');
}
}
and inside config file, add 'app\components\Aliases' to bootstrap array
'bootstrap' => [
'log',
'app\components\Aliases',
],
In config folder create file aliases.php. And put this:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '#webroot/scripts/sceditor/languages/');
In web folder in index.php file put:
require(__DIR__ . '/../config/aliases.php');
Before:
(new yii\web\Application($config))->run();
If run echo in view file:
echo Yii::getAlias('#editor_lang_dir');
Show like this:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
#webroot alias is not available at this point, it is defined during application bootstrap :
https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60
No need to define this alias yourself, you should simply use another one :
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#app/web/scripts/sceditor/languages/',
],
To improve on #vitalik_74's answer
you can place it in config/web.php instead(if you are using the basic yii app, I'm not sure about the main config file in the advance version, but the same applies, just put the require on the main config file) so that it gets shorten to:
require(__DIR__ . '/aliases.php');
Using the config.php always_load configuration, how does one load a language file from a package?
All of the fuelphp documentation alludes to being able to do this, but only shows the syntax for loading from a module.
Here's what I'm trying to do:
fuel/app/config/config.php
'always_load' => [
'language' => [
// loads fuel/app/lang/en/login.php into login group
'login',
],
],
fuel/app/config/production/config.php
'always_load' => [
'language' => [
// override /config/config.php with contents from
// /fuel/packages/pkg/lang/en/login.php
'lang_file_from_package' => 'login',
],
],
Packages are core extensions, which means it will merge the contents of the files found in app and in the package.
As such, there is no method to define you want to load it from the package only, other then by specifying a fully qualified pathname, which will always load just that file.
I'm following the method described here to have my control panel in backend folder.
The problem is I don't want to create two assets folders so I want to set assetManager baseUrl property in my backend/config/main.php but I'm enable to get the URL of the backend in the config. I don't want it hard coded because I need the code to be portable.
What can I do?
Thanks.
You can configure asset manager's baseUrl in /backend/config/main.php
'components' => array(
...
'assetManager' => array(
'class' => 'CAssetManager',
'baseUrl' => 'frontend.assets'
),
...