Controller mapping causing error in yii2 console application - php

Module section config
'user' => [
'class' => 'dektrium\user\Module',
'modelMap' => [
'User' => 'app\models\DL\User',
'registrationForm' => 'app\models\DL\registrationForm',
],
'controllerMap' => [
/*'registration' => 'app\controllers\user\RegistrationController',
'admin' => 'app\controllers\user\AdminController'*/
],
'layout' => '#app/views/layouts/container',
'defaultRoute' => 'profile',
'admins' => ['admin'],
'enableFlashMessages' => false,
'params' => [
'menuItems' => [
'label' => 'Users',
'url' => ['/user/admin']
]
]
],
Yii console application (./yii) showing me error
'Calling unknown method:
app\controllers\user\AdminController::getHelpSummary()'
If I uncomment the controllerMap section, I can't understand why it autoloads in console app if my AdminController extends web controller not console.
This is commands from user module.

Do you really need the user module in console?
Yii2 console and web applications have separated configuration files by default. If you changed this default and use the same config for both of them, you must take care about consistency.
You can check the list of loaded configs in ./yii.

You need to specify a valid defaultRoute for the console application.
With 'defaultRoute' => 'profile', ./yiimay try to load a Controller which requires the user module.
Try adding it in the console configuration.

Related

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.
],
],
],
],

Yii DbMessageSource App* Error

I am currently building a second system with yii2. It will use some tables from a Yii1 database for translation. The Yii1 project was originally translated using files but this has now been moved to the db. All the translations of the Yii1 system use one of three categories app,flash,email of which the vast majority use app.
In the Yii2 project I have the following in the web.php config file.
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable' => 'translation_source',
'messageTable' => 'translation',
'forceTranslation'=>true,
],
],
],
All of the translations on the system that use app are not translated, however, all other categories are. If I change the above code to
'i18n' => [
'translations' => [
'app*' => [
Then I get an error for other categories but not app and the app strings are translated as expected. The error I get is
Unable to locate message source for category 'flash'.
If however I change my config to the following, this works for all translations.
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable' => 'translation_source',
'messageTable' => 'translation',
'forceTranslation' => true,
],
'app*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable'=>'translation_source',
'messageTable' => 'translation',
'forceTranslation' => true,
],
],
],
It just seems odd that I am having to include effectively the same code block twice. Can anyone tell me how I can achieve this in one array or is this the expected behaviour?
** Update **
I do see some mention of * in the docs Docs. I also see some mention of this in the Forum

how to get yii2/admin menu

i am new in yii2 and using advance template .
i am using yii2/admin for roles , permission. but i can't get the menu manage using yii2/admin
the image for the menu manage
like this
how to get this interface for manage menu
i read this
when i run
(index.php?r=admin/menu)
i got an error
Invalid Configuration – yii\base\InvalidConfigException
The table does not exist: {{%menu}}
how to create menu table.
i got a migration file from
vendor/mdmsoft/yii2-admin/migrations/m140602_111327_create_menu_table.php
how to run this migration
please go through with this link MDM Yii2-admin Basic Configuration
add in backend/congif/main.php
'modules' => [
'admin' => [
'class' => 'mdm\admin\Module',
'layout' => 'left-menu', // it can be '#path/to/your/layout'.
'controllerMap' => [
'assignment' => [
'class' => 'mdm\admin\controllers\AssignmentController',
'userClassName' => 'common\models\User',
'idField' => 'id'
],
],
'menus' => [
'assignment' => [
'label' => 'Grand Access' // change label
],
'route' => null, // disable menu route
]
],
],
for run migration use this, through cmd/terminal
yii migrate --migrationPath=#mdm/admin/migrations

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 Elasticsearch db configuration

I have yii2 installed with Elasticsearch, Logstash, Kibana. The set-up is working well except for the interaction with YII2.
Below is the contents for ./config/db.php
<?php
return [
'components' => [
'db' => [
'class' => 'yii\elasticsearch\Connection',
'user' => 'admin',
'password' => 'admin',
'nodes' => [ ['http_address' => '127.0.0.1:9200'],
// configure more hosts if you have a cluster
],
],
]
];
This configuration is not working, below are the first lines of the error.
Invalid Configuration – yii\base\InvalidConfigException
The configuration for the "db" component must contain a "class"
element.
in /var/www/hostings/yiiphp/tools/vendor/yiisoft/yii2/di/ServiceLocator.php
.....
I fail o find relevant information to fix this. What am i doing wrong ?
file ./config/db.php should be included by the main config file, named web in your case (it seems that you are using basic app template)
Another words - look at file ./config/web.php then find the line:
'db' => ... and replace it with 'db' => require(__DIR__ . '/db.php'),
Finally, your db.php should be like this:
<?php
return [
'class' => 'yii\elasticsearch\Connection',
'user' => 'admin',
'password' => 'admin',
'nodes' => [ ['http_address' => '127.0.0.1:9200'] ]
];
It is also seems strange to me, that you want to use elastic component as a db component, but its up to you, of course.

Categories