How to add things under the namespace App in laravel 8 - 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/",
}

Related

intellij doesn't recognize babelrc alias path when it import file included in that path- warning module is not install

This is my babelrc file. installed module-resolver and declared root and aliases.
actually this do works!! but underline annoying me... please click images below I cannot post images cause I'm new here.
[https://i.stack.imgur.com/ZzN5O.png]
warning like this - module is not installed i think intelliJ recognize it as module..
[https://i.stack.imgur.com/GTcWx.png]
I changed my root "./" -> "./src" but it didn't work.
I also installed eslint but don't know about that well I think that won't help this problem
has anyone solved this kind of issue before?
IDEA provides no special support for babel-resolver; please follow WEB-28241 for updates.
The problem is that there are dozens of plugins defining their own ways to resolve modules, we can't afford providing special support for all them... You can try using webpack aliases instead, or try a workaround from https://youtrack.jetbrains.com/issue/WEB-22717#focus=streamItem-27-1558931-0-0:
create a file config.js (you can use a different name if you like) in your project root dir
define your aliases there using the following syntax:
System.config({
"paths": {
"components/*": "./src/components/*"
}
});
components here is the alias you have defined in .babelrc
Now you can use imports like
import MyComponent from 'components/core/MyComponent';
in your code

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.

PHPUnit autoloader error: Class 'Yii' not found

I am trying to start PHPUnit testing.
I am using composer to load PHPUnit 4.5 , Yii 1.1.14 and some custom Yii packages that we have built.
Inside those custom packages, we autoload some files that set some aliases using the Yii class.
When running our application, we include the base Yii file manually, and then run the composer generated autoloads.
The trouble is, when we run PHPUnit.. the composer autoloads get run first. Even when specifying a bootstrap file with the include:
bin/phpunit --bootstrap carcass/phpunit.bootstrap.php
Leading to the following Exception:
Fatal error: Class 'Yii' not found
In fact it appears the autoloads are run even before the -- options are parsed:
bin/phpunit --help
results in the same error. Removing the autoloads allows PHPunit to run.
Is there any way around this?
I tried placing an autoload for the Yii base file in our main composer.json, but the sub-packages' autoloads get run first.. same error.
I also tried placing an autoload for the Yii base file in each of the sub-packages.. but then we get redeclaration errors as composer uses require. I'm also not a massive fan of this option as it rigidly defines where the Yii definition comes from to sub-packages that don't really need to know.
As the autoload classmap section is run first before all the files sections (including those from the sub-packages).
Placing the yii and YiiBase files in the classmap of the main composer.json for our project solved this issue:
"autoload": {
"classmap": [
"composer_packages/yiisoft/yii/framework/YiiBase.php",
"composer_packages/yiisoft/yii/framework/yii.php"
],
"files": [
...
]
}

d.ts interface augmentations being recognized by IDE but not by TSC

I am using ES6 style module inclusion within webstorm and I have an express app which has a custom d.ts (For middleware which alters objects) which looks something like this:
declare module Express {
export interface Application {
getLogger(): LoggerInstance;
getRepository(collectionName): IRepository;
getEnvironmentVars(): any;
}
}
Now the IDE sees this and whenever I were to use it there are no errors, however when I run this through TSC it blows up saying 'getRepository' does not exist on type 'Application'
I have plenty of other d.ts files which are working fine but there are no errors listed and nothing odd I can see there.
An example use case of this may be:
import { Express } from "express"
export function SomeMiddleWare(app: Express){...};
(Express in the express.d.ts extends Application which contains the augmentations in the other file.)
So I am baffled as to why it works in the IDE but not in the TSC, which is including all the *.d.ts files within a typing directory, there are no errors being listed with the d.ts files just with the usages which are defined in this specific d.ts.
TSC it blows up saying 'getRepository' does not exist on type 'Application'
Make sure that this new .d.ts is included in the compilation context for tsc using something like tsconfig.json

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!
}