Yii2 Custom / Shorter Namespace - module

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.

Related

How to add things under the namespace App in laravel 8

What do I have to do to be able to use App\Traits\Uuids;
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use app\Traits\Uuids;
The uuids trait is under ..\app\Traits\Uuid.php so it only works if use app\Traits with a small cap.
If I use the namespace
namespace App\Traits;
instead of
namespace app\Traits;
I am using Composer version 2.1.3 2021-06-09 16:31:20 and when I run
composer dump-autoload
it will returns
"Class App\Traits\Uuids located in [...]/app/Traits\Uuid.php does not comply with psr-4 autoloading standard. Skipping."
In addition to matching the caps, as suggested, the filename must match the class name.
renaming the php file to Uuid.php removed the psr-4 error.
composer.json has a provision that renames App to app/ and that part works out of the box.
"autoload": {
"psr-4": {
"App\\": "app/",
}

Autoload module class in Prestashop

I've created a module that has an override for the FrontControllerCore class to add additional Smarty variables.
The issue I'm running into is trying to autoload a class that is referenced in the controller that is in my module. The class isn't being loaded and I don't know how to add it to the autoloader.
When you install the module the FrontController.php file should be located in:
override\classes\controller\
so from the FrontController.php you can "include" manually that file like:
require_once(dirname(__FILE__).'/../../../modules/servicecharges/classes/ServiceCharge.php');
There's no autoload for such includes.
Also you can use this free tiny module that override Prestashop autoload. After that all your module custom class will be autoloaded.
Exemple path: /modules/my_module/libs/classes/MyClass.php
Extended Api
I've been able to solve a similar problem, with Composer's autoload. Way to require an autoload in one file on a Prestashop module?
Instead of overriding a controller (leading to conflicts with other plugins or installations of Prestashop already using the same overrides) you could invoke the hook moduleRoutes.
This way, you could call your autlooader always before the controllers:
<?php
public function hookModuleRoutes() {
require_once __DIR__.'/vendor/autoload.php'; // And the autoload here to make our Composer classes available everywhere!
}

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 change yii config file via a module

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.

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.