Yii 2: Unable to find a class into the extension 'yii2-admin' - php

Extension's Github project: https://github.com/mdmsoft/yii2-admin
I'm using the advanced template of yii2, so I've backend, frontend and common folders all into an advanced folder.
the advanced folder itself is on the same level of vendor folder.
I've others extensions and all is working
Then I installed yii2-admin by composer
php composer.phar require mdmsoft/yii2-admin "*"
Installation worked well.
Into vendor folder I've now mdmsoft folder with yii2-admin subfolder into it.
This is the actual content of vendor/yiisoft/extensions.php (modified by composer installation, I haven't manually touched it)
<?php
$vendorDir = dirname(__DIR__);
return array (
'yiisoft/yii2-jui' =>
array (
'name' => 'yiisoft/yii2-jui',
'version' => '2.0.0.0',
'alias' =>
array (
'#yii/jui' => $vendorDir . '/yiisoft/yii2-jui',
),
),
'mdmsoft/yii2-admin' =>
array (
'name' => 'mdmsoft/yii2-admin',
'version' => '1.0.2.0',
'alias' =>
array (
'#mdm/admin' => $vendorDir . '/mdmsoft/yii2-admin',
),
),
);
I added this configurations to common/config/main.php
<?php
return [
...
'components' => [
...
'authManager' => [
'class' => 'yii\rbac\DbManager', // or use 'yii\rbac\PhpManager'
],
],
];
And these configs adedd to backend/config/main.php
'modules' => [
'admin' => [
'class' => 'mdm\admin\Module',
]
],
'components' => [
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
'admin/*', // add or remove allowed actions to this list
]
],
],
(I omitted useless code in every code block)
I also have enabled pretty url. And it was working.
Complete error stack
2014-11-06 17:05:49 [127.0.0.1][-][-][error][ReflectionException] exception 'ReflectionException' with message 'Class mdm\admin\Module does not exist' in C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\di\Container.php:408
Stack trace:
#0 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\di\Container.php(408): ReflectionClass->__construct('mdm\\admin\\Modul...')
#1 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\di\Container.php(354): yii\di\Container->getDependencies('mdm\\admin\\Modul...')
#2 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\di\Container.php(147): yii\di\Container->build('mdm\\admin\\Modul...', Array, Array)
#3 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\BaseYii.php(344): yii\di\Container->get('mdm\\admin\\Modul...', Array, Array)
#4 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\base\Module.php(354): yii\BaseYii::createObject(Array, Array)
#5 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\base\Module.php(511): yii\base\Module->getModule('admin')
#6 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\base\Module.php(449): yii\base\Module->createController('admin/route')
#7 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('admin/route', Array)
#8 C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#9 C:\xampp\htdocs\advanced\backend\web\index.php(18): yii\base\Application->run()
#10 {main}
The problem
If I open my backend at /admin I got this exception
ReflectionException Class mdm\admin\Module does not exist
It's my first Yii2 app, so I've no idea of what's the problem and how to debug it.
Edit 1
I checked C:\xampp\htdocs\vendor\mdmsoft\yii2-admin\Module.php and in its content there is the Module class definition
class Module extends \yii\base\Module
EDIT 2
My first error is that asAccess was put into components array. But It must be outside of it, at the same level
Now the error is changed:
Class mdm\admin\components\AccessControl does not exist

First: this is wrong
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
// 'admin/*', // add or remove allowed actions to this list
]
],
],
It must be as here:
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
// 'admin/*', // add or remove allowed actions to this list
]
],
Second: you must run this command
php composer.phar require mdmsoft/yii2-admin "*"
from within the app root (htdocs\advanced), NOT from where you have executed the installation of yii (you were into htdocs). Only in this way to install yii2-admin into your app
Third: it's not true that console has not authManager ! if you have to use rbac, to simply do the default yii2 migration you must have it configured into console app. So my suggestione is: configure authManager into console, and other things into frontend and or backend where you need it
Fourth: you cannot admin frontend route from backend, so you need to enable this module even into frontend

"I added this configurations to common/config/main.php"
NOTE: !! Dont put at common, put in frontend or backend... console application has no user component and dont need access control.
Try adding this in backend/config/main right before 'modules'
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'extensions' => require(__DIR__ . '/../../vendor/yiisoft/extensions.php'),
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
],
and I can't but assume that the whole thing has return in front, so the whole code is like:
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'extensions' => require(__DIR__ . '/../../vendor/yiisoft/extensions.php'),
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
],
'modules'=>[
'admin' => [
'class' => 'mdm\admin\Module',
],
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
'admin/*',
],
],
];
Hope this helps :D

Related

can't view backend user admin pages using Yii2-usuario

I'm using Yii2-usuario for my user module.
I ran the migrations found in "first step" under the section "Creating the first Administrator during a migration", and only changed from new \Da\User\Model\User() to new \app\models\user\Model\User() like this
$user = new \app\models\user\Model\User([
'scenario' => 'create',
'email' => "admin#admin.com",
'firstname' => 'first',
'lastname' => 'last',
'password' => "verysecret" // >6 characters!
]);
it populated my tables correctly. But when i login to backend and try to view https://localhost/bla/backend/web/user/admin/index, i get a 403 forbidden error
in my backend main.php i have this
'components' => [
....
'authManager' => [
'class' => 'Da\User\Component\AuthDbManagerComponent',
'defaultRoles' => ['guest'],
],
],
'modules' => [
'user' => [
'class' => Da\User\Module::class,
'enableEmailConfirmation' => true,
'enableRegistration' => false,
'maxPasswordAge' => 90,
'enableGdprCompliance' => false,
'classMap' => [
'User' => 'app\models\user\Model\User',
],
'viewPath' => '#app/views/user',
'controllerMap' => [
//disable for backend
'profile' => [
'class' => Da\User\Controller\ProfileController::class,
'as access' => [
'class' => yii\filters\AccessControl::class,
'rules' => [['allow' => false]],
],
],
'recovery' => [
'class' => Da\User\Controller\RecoveryController::class,
'as access' => [
'class' => yii\filters\AccessControl::class,
'rules' => [['allow' => false]],
],
],
'Registration' => [
'class' => Da\User\Controller\RegistrationController::class,
'as access' => [
'class' => yii\filters\AccessControl::class,
'rules' => [['allow' => false]],
],
],
'Settings' => [
'class' => Da\User\Controller\SettingsController::class,
'as access' => [
'class' => yii\filters\AccessControl::class,
'rules' => [['allow' => false]],
],
],
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationNamespaces' => [
'Da\User\Migration',
],
'migrationPath' => [
'#app/migrations',
'#yii/rbac/migrations',
],
],
],
],
my User model in backend\models\user\Model looks like this
use Da\User\Model\User as BaseUser;
class User extends BaseUser
{
public static function tableName()
{
return '{{%admin}}';
}
...
...
..
}
the list of RBAC and admin action don't work. i get a 403.
any idea what I'm missing here or did wrong? Thanks.
In the link which was provided for the first step with migration code, next is written
After installing the extension and having configured everything, you
need setup your application with the all the user related stuff, e.g.
You need to run this migration only after you did all installation steps mentioned here. But still it won't work because default user table will be populated, which was created from initial migration. This package creates its own user table and moreover in installation steps there is a Note
Note: If you are using Yii2's Advanced Application Template, before
starting to work with database, please ensure you have deleted
m130524_201442_init.php migration file which comes from the default
installation. It's located at
%PROJECT_DIR%/console/migrations/m130524_201442_init.php path.
Step 1
In your case i would do yii migrate/down 2, which will revert last 2 migrations(init migration contain user table description). Only in case if you didn't add more migrations :)
Total 2 migrations to be reverted:
m190124_110200_add_verification_token_column_to_user_table
m130524_201442_init
In case migration fail, you can run few SQL queries
drop table user;
delete from migration where version='m130524_201442_init';
and then delete m130524_201442_init.php and m190124_110200_add_verification_token_column_to_user_table.php(second one is optional but its your call) files
Step 2
After that according to docs, you need to run rbac + Yii 2 Usuario migrations all together as stated in this note
Note: You will still have to apply Yii 2 RBAC migrations by executing
./yii migrate --migrationPath=#yii/rbac/migrations. Remember that you
have to configure the AuthManager component first. Also, namespaced
migrations were introduced in Yii 2.0.10, so before using them
consider updating your framework installation version. If you are
using a Yii 2 version prior to 2.0.10, you'll have to copy the
migrations located on vendor/2amigos/yii2-usuario/src/User/Migration,
remove its namespaces and add it to your #app/migrations folder.
But before that, you need to move code below from backend/config/main.php to %PROJECT_DIR%/console/config/main.php
'controllerMap' => [
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationNamespaces' => [
'Da\User\Migration',
],
'migrationPath' => [
'#app/migrations',
'#yii/rbac/migrations',
],
],
]
and add authManager into console config for rbac also
'authManager' => [
'class' => 'Da\User\Component\AuthDbManagerComponent',
],
in final your console/config/main.php should be similar to this
return [
// ....
'controllerMap' => [
// ...
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationPath' => [
'#app/migrations',
'#yii/rbac/migrations', // Just in case you forgot to run it on console (see next note)
],
'migrationNamespaces' => [
'Da\User\Migration',
],
],
],
'components' => [
'authManager' => [
'class' => 'Da\User\Component\AuthDbManagerComponent',
],
// ...
],
// ...
];
Step 3
Run the migration from note
./yii migrate --migrationPath=#yii/rbac/migrations
Total 13 new migrations to be applied:
Da\User\Migration\m000000_000001_create_user_table
Da\User\Migration\m000000_000002_create_profile_table
Da\User\Migration\m000000_000003_create_social_account_table
Da\User\Migration\m000000_000004_create_token_table
Da\User\Migration\m000000_000005_add_last_login_at
Da\User\Migration\m000000_000006_add_two_factor_fields
Da\User\Migration\m000000_000007_enable_password_expiration
Da\User\Migration\m000000_000008_add_last_login_ip
Da\User\Migration\m000000_000009_add_gdpr_consent_fields
m140506_102106_rbac_init
m170907_052038_rbac_add_index_on_auth_assignment_user_id
m180523_151638_rbac_updates_indexes_without_prefix
m200409_110543_rbac_update_mssql_trigger
Step 4
Create and run migration from first steps without changing model in example.
Step 5
Remove user config from both backend/config/main.php and frontend/config/main.php
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
Apply authManager and user module with proper administrators option to respective config files. This array should contain same role name as in migration which you did in step 4.
'modules' => [
'user' => [
'class' => Da\User\Module::class,
'administrators' => ['admin']
],
],
'authManager' => [
'class' => 'Da\User\Component\AuthDbManagerComponent',
],
In the final your frontend and backend config files should be similar to this
return [
// ...
'modules' => [
'user' => [
'class' => Da\User\Module::class,
'administrators' => ['admin']
],
],
'components' => [
'authManager' => [
'class' => 'Da\User\Component\AuthDbManagerComponent',
],
// ....
]
// ...
];
Step 6
My favorite step :)
http://yourapp/index.php?r=user/admin visit and enter your creds from migration. Enjoy!

Laminas - Unable to render template "xxxxxxx" resolver could not resolve to a file

This project has been ported over manually from Zend Frame work 2 to Laminas. The issue here is that the module.config.php has been set up in the same way as other modules that are working. However I am encountering this error. I have checked the usually culprits such as files spelling or missing, no other modules are using the same route name. Is there another part of Laminas that would affect the view manager?
The modul.config.php setup is below.
'''
namespace ProjectTaskDocument;
use Laminas\Router\Http\Segment;
return [
'router' => [
'routes' => [
'project-task-document' => [
'type' => Segment::class,
'options' => [
'route' => '/task-document[/:action][/:id]',
'constraints' => [
'action' => 'index|add|download|view-all|delete'
],
'defaults' => [
'controller' => Controller\ProjectTaskDocumentController::class,
'action' => 'index'
]
]
]
]
],
'view_manager' => [
'template_path_stack' => [
'ProjectTaskDocument' => __DIR__ . '/../view'
],
]
];
'''
The module folder structure
Realised my mistake, during my port process another module was using the same Key identifier for the view_manager->template_path_stack. I feel pretty damn dumb for missing this.

EAuth Extension Yii2, Class nodge\eauth\EAuth does not exist

I am trying to use EAuth extension in Yii2 advanced application. I installed it through composer and it installed it under vendor directory and configured as mentioned here (did the config in common/config/main). So, following is the directory structure for that:
root
vendor
nodege
lightopenid
provider
*files go here*
yii2-eauth
src
*files go here*
EAuth.php is under src folder. In controller I have done this in use:
use nodge\eauth\EAuth;
and when I do this:
$eauth = Yii::$app->get('eauth')->getIdentity($serviceName);
I get the following error:
Class nodge\eauth\EAuth does not exist
What am I doing wrong? Any help?
Update:
This is my config:
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'eauth' => [
'class' => 'nodge\eauth\EAuth',
'popup' => true, // Use the popup window instead of redirecting.
'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache' on production environments.
'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.
'httpClient' => [
// uncomment this to use streams in safe_mode
//'useStreamsFallback' => true,
],
'services' => [// You can change the providers and their classes.
'google' => [
// register your app here: https://code.google.com/apis/console/
'class' => 'nodge\eauth\services\GoogleOAuth2Service',
'clientId' => '...',
'clientSecret' => '...',
'title' => 'Google',
],
'facebook' => [
// register your app here: https://developers.facebook.com/apps/
'class' => 'nodge\eauth\services\FacebookOAuth2Service',
'clientId' => '---',
'clientSecret' => '---',
],
'yahoo' => [
'class' => 'nodge\eauth\services\YahooOpenIDService',
//'realm' => '*.example.org', // your domain, can be with wildcard to authenticate on subdomains.
],
],
],
],

Yii2 API routing with versions

I'm having a problem getting my Yii2 application API setup. We have a website up and running and I've been tasked to setup a API for 3rd parties to connect to us to perform certain function calls. I've been reading the docs and did some googling and found this site that has a base setup for website and api. I installed it to see how it was setup, so I could try and apply it to my site.
I'm hitting the API section of the directory structure just fine, but I can't for the life of me figure out how the routing is supposed to work.
My directory structure is now as follows:
root
--api
----config
------main.php
----modules
------v1
--------controllers
----------SearchController.php
--------models
----------Search.php
----------ApiUser.php
--------Module.php
----runtime
----web
------assets
--assets
--commands
--config
---common
---site1
--controllers
----base
----common
----site1
--mail
--migrations
--models
--modules
--runtime
--vendor
----vendor_dirs
--views
--web
My apache config is as follows for the api alias:
Alias /api /var/www/website.com/api/web
<Directory "/var/www/webiste.com/api/web">
AllowOverride All
</Directory>
I'm confused to as to how I'm supposed to setup the url_manager section of the config file so that www.website.com/api/v1/search/do-search will hit the \api\modules\v1\controllers\SearchController::actionDoSearch() function.
My config looks as follows
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'modules' => [
'v1' => [
'basePath' => '#app/modules/v1',
'class' => 'api\modules\v1\Module',
'controllerNamespace' => 'api\modules\v1\controllers',
],
],
'components' => [
'user' => [
'identityClass' => 'api\v1\models\ApiUser',
'enableAutoLogin' => false,
'enableSession' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'info', 'trace'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/search',
'pluralize' => false,
'extraPatterns' => [
'GET do-search' => 'do-search'
]
],
],
],
],
I've read through the routing guide on the Yii2 website, but it didn't really shed any light on the subject.
update
So after tweaking my config, I think I managed to make some headway, but I'm still not there yet.
I'm now getting the following error:
ReflectionException
Class api\modules\v1\Module does not exist
My namespace in the module is as follows:
namespace api\modules\v1;
class ApiModule extends \yii\base\Module
{
So it seems that the namespace is not registering property, or the base path for the module is not correct.
Any help explaining things, so I can better understand would be greatly appreciated.
Thanks
SUCCESS
I'm an idiot. The Module.php file was not in the v1 directory, but actually one directory down.
Add your module to modules section:
'modules' => [
'v1' => [
'class' => 'app\modules\v1\Module',
'basePath' => '#app/modules/v1',
'controllerNamespace' => 'app\modules\v1\controllers'
]
Add url rules for REST controller:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/search']
],
'GET v1/search/do-search' => 'v1/search/do-search',//actually should work without this line
When creating new controller add it to controller section in url rules.

Yii2 - Multi language

I'm trying to set up the website's frontend translation using the i18l thing. Here is my i18l.php file placed on frontend/config
<?php
return [
'sourcePath' => 'frontend',
'languages' => ['en-US', 'pt-BR'] , //Add languages to the array for the language files to be generated.
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => false,
'only' => ['*.php'],
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/vendor',
],
'format' => 'php',
'messagePath' => 'frontend' . DIRECTORY_SEPARATOR . 'translations',
'overwrite' => true,
];
and here my main.php also on frontend
(...)
'language' => 'en-US',
'components' => [
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => 'frontend/translations',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
],
]
I'm using the <?= Yii::t('app', 'some string') ?> on the sites and layouts and when I run the command ./yii message/extract #frontend/config/i18n.php it creates to me a folder called 'translations' contain other two folders 'en-US' and 'pt-BR' both with app.php which i already had filled with some translations. But still, no translation happens when i change the language on the main.php as it should be (i think).
I would appreciate if someone could give me a hand on that.
Thanks.
Great post, with all the needed details.
I was struggling with the same things, but you did it quite well.
So, if you can run the command and it generates the file, then the sourcePath is correct.
If it doesn't display the translation messages at runtime, despite your setting changes, then, I presume the issue could be on your basePath:
Try using, on your basePath configuration, the following:
'basePath' => '#frontend/translations',

Categories