How to change yii config file via a module - yii

Is it possible to change config file via a module in Yii ?
I want to add some items to module array in config/main.php
Thank you.

Create file in protected/modules/moduleName/config/main.php with such a content
<?php
return array(
'components'=>array(
....
),
'title'=>'My module News'
);
And it will be fetched automatically.

Related

Yii2 Custom / Shorter Namespace

I have a nested 'mail' module in my yii2 (basic template) at this location:
#app/modules/admin/modules/mail
How do I create shorter namespaces in all of the modules files. So instead of this namespace in my controller files:
namespace app\modules\admin\modules\mail\controllers;
I could just have:
namespace mail/controllers;
If I ever move the module folder, I wouldn't have to go and manually change the namespace in every single file (also they're just long).
The docs actually recommend this here http://www.yiiframework.com/doc-2.0/guide-structure-modules.html#nested-modules where it says "you should consider using a shorter namespace here!"
But how do you accomplish this?
you must set alias to directory at bootstrap to custom namespace.
First, create a bootstrap.php in config/ folder:
//bootstrap.php
Yii::setAlias('mail', dirname(dirname(__DIR__)) . '/modules/admin/modules/mail');
Add run bootstrap.php at init app.
Edit file web/index.php, add this line after require Yii.php
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
//Add after require Yii.php
require(__DIR__ . '/../config/bootstrap.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
Now you can set namespace for controllers in mail module is mail/controllers.
Hope it helpful.

How can I customize yii2 theme

Im just trying Yii2. Now I have created a yii2 basic appication. Next what I want to do is change the theme. I have an HTML file I want to change this appications theme as like that HTML. I have gone through yii2 theming But Its not what i want I want to add all css,js,images,font of that HTML to my project. How can I do this in yii2 Plz somebody help me.
Hi you have to do following things for theming in yii2 as simple as yii1 :-
first of all open web.php into config directory of yii2 application,
then in component array pass view array like:
'view' => [
'theme' => [
'pathMap' => [
'#app/views' => [
'#webroot/themes/demo/views',
]
],
],
] // here demo is your folder name
now create the folder name as "themes" into web directory.
In this themes folder copy your html folder like(demo) which contains all css, js etc. files. in this folder create views folder as yii which contains main.php and others layouts if needed.
override index.php in views by your corresponding index file
by appropriate changes into paths of files. create corresponding views and
actions for your html files.
in yii2 we can define our css and js into Appasset.php .
Hi you need to do a few things to start off.
Copy the new HTML template Css, JS, Plugins and Images into your project directory eg testdrive/assets/
We need to link the CS and Js files to the project. Go to testdrive/protected/views/layouts/ directory and create a new file eg main.php add the respective css and js links to the respective files as you would on a html file in the head section.
<head>
<link href="<?php echo Yii::app()->request->baseUrl; ?>/assets/css/style.css" rel="stylesheet">
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body><? php echo $content ?></body>
3.Once the layout is ready we need to initialise yii to use the layout we have created. Proceed to the Controller eg SiteController.php
public function actionlogin{
$this->layout = 'main';
}
Now you're set to get your hands dirty.

Best way to extend a module in zend framework 2

What is best way to extend zend framework 2 modules.
For example I want to change register page view in zfcuser , I cloned zfcuser module from vendor to modules folder and changed view files but Zend Framework 2 uses vendor folder one, If I delete zfcuser folder in vendor it works ok but two modules at same time uses vendor version
You don't have to clone the module. You can just create a zfc-user/user folder in your own module's view folder. And then place your own register.phtml in that folder. Just make sure that ZfcUser is included before your own custom module and then your own custom view should be loaded.
Edit: this is a link to the official docs on this topic: ZfcUser custom view scripts
If you only want to override template files just override the module.config.php in your Modul directory.
'view_manager' => array(
'template_path_stack' => array(
'zfcuser' => __DIR__ . '/../view',
),
),
Within your view directory add a folder called zfc-user and add a register.phtml file. Within that template file feel free to change whatever you want, just keep in mind that you still have to keep the form action as it is.

How to get the file upload button to show in the EditMe extension

I'm using the ExtEditMe extension in my project with the following configuration:
$this->widget('ext.editMe.widgets.ExtEditMe',
array(
'model'=>$model,
'attribute'=>'Text',
'ckeConfig'=>array('enableTabKeyTools'=>true,'enterMode'=>2),
'height'=>'500',
'width'=>'100%',
'filebrowserImageBrowseUrl'=>'/protected/extensions/kcfinder/browse.php?type=files',
'filebrowserImageUploadUrl'=>'/protected/extensions/kcfinder/upload.php?type=files',
'filebrowserBrowseUrl'=>'/protected/extensions/kcfinder/upload.php?type=files',
'filebrowserUploadUrl'=>'/protected/extensions/kcfinder/upload.php?type=files'
)
);
For some reason the file upload button isn't showing in the tool bar. Is there something else I need to add?
Questions too much for a comment. Could you check the following:
Check the permissions for the assets folder. Need to be writable by your
httpd (example apache:apache or apache:www-data)
EditMe should be inside the protected/extensions/ folder
Change your widget call to:
$this->widget('application.extensions.editme.widgets.ExtEditMe',
(used full path)
Check if kcfinder/config.php says: 'disabled' => false,
Also, there may be a need to put kcfinder in the root of the webapp. See this example with CKeditor.
The following comes from the CKEDITOR docs:
This button will be hidden by default (hidden:true). The filebrowser plugin looks for all elements with the filebrowser attribute and unhides them if appropriate configuration setting is available (filebrowserBrowseUrl/filebrowserUploadUrl).
Which could mean your config settings for the filebrowser plugin are not correct. You should try the following:
KCFinder (your filebrowser plugin) is not a yii extension as far as I know, so move the kcfinder folder to the root folder (or any other folder outside of the protected folder).
Change the paths to the KCFinder files. I recommend using yii's Yii::app()->baseUrl to make sure you're getting the correct paths:
'filebrowserImageBrowseUrl'=>Yii::app()->baseUrl.'/kcfinder/browse.php?type=files',
'filebrowserImageUploadUrl'=>Yii::app()->baseUrl.'/kcfinder/upload.php?type=files',
'filebrowserBrowseUrl'=>Yii::app()->baseUrl.'/kcfinder/upload.php?type=files',
'filebrowserUploadUrl'=>Yii::app()->baseUrl.'/kcfinder/upload.php?type=files'
Hope that helps.
'filebrowserImageBrowseUrl'=>'/protected/extensions/kcfinder/browse.php?type=files',
'filebrowserImageUploadUrl'=>'/protected/extensions/kcfinder/upload.php?type=files',
'filebrowserBrowseUrl'=>'/protected/extensions/kcfinder/upload.php?type=files',
'filebrowserUploadUrl'=>'/protected/extensions/kcfinder/upload.php?type=files'
These URLs will be passed to the Javascript initiator of the extension, and they will be requested from there, so you can copy them and try requesting them from the browser to see if they are working (I'm almost sure they won't work).
One way you can work this out, is by creating a controller and includes these PHP files there, then make these URLs refer to the controller you created. Hope it helps.

Yii - how to include phpdocx files?

I'm trying to include phpdocx files in my Yii project. On a standalone php script, all I have to do is
require_once 'phpdocx/classes/CreateDocx.inc';
Not sure how to do it with Yii. I put the folder under protected/components/thirdparty/phpdocx. And added the line above (with correct path) in config/main.php, but Yii can't find the rest of the .inc files.
How do I include those?
Try this:
define('PHPDOCX_INCLUDE_PATH', (dirname(Yii::app()->basePath)).'/components/thirdparty/phpdocx');
spl_autoload_unregister(array('YiiBase','autoload'));
require_once PHPDOCX_INCLUDE_PATH.'/classes/AutoLoader.inc';
spl_autoload_register(array('AutoLoader','load'));
spl_autoload_register(array('YiiBase', 'autoload'));
// your code
$docx = new CreateDocx();
$docx->addTemplate('files/mytemplate.docx');
You need to use the namespacing version of Phpdocx, and then just load the library in composer.json.