yii2 map extension showing "MissingKeyMapError" - google-maps

I have install extension from http://www.yiiframework.com/extension/yii2-latlon-finder/
but is showing me error message "MissingKeyMapError"
I have API key but not sure where to put this.
please help me for same

As i can see, this project is last updated 2 years ago, so it can be outdated with current API's.
As a solution, i can suggest you to use 2amigos - Google Maps extension. It's pretty simple with good documentation.
After installing it, just add this to main.php config file:
'components' => [
'assetManager' => [
'bundles' => [
'dosamigos\google\maps\MapAsset' => [
'options' => [
'key' => 'this_is_my_key',
'language' => 'id',
'version' => '3.1.18'
]
]
]
],
],
Just replace this_is_my_key with your key.
The bad thing in this solution is that you will have to write some additional code to handle user input of lat/long and display it on 2amigos map.

Related

Yii2 internationalizating slug and controller

I'm making a yii2 site in 2 languages (for now) using yii's native i18n module, but how can I add multilanguage support for the action URLs?
For instance one of my actions is category/slug and in English, it will show as
http://example.com/category/chair
but in spanish it has to be shown as
http://example.com/categoria/silla
and so on for other languages shown in the future
right now im manually adding my routes like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:\w+>' => 'site/<alias>',
'marca/<slug>' => 'site/brand',
'categoria/<slug>' => 'site/category',
'brand/<slug>' => 'site/brand',
'category/<slug>' => 'site/category',
],
],
Do i need to manually route every action to its correct controller or is it possible to add a more automated form using yii::t() function?
you will need to write your own UrlRule class, implementing yii\web\UrlRuleInterface and configure your UrlManager.
read more here.
basically it is about "translating" a given request like '/categoria/silla' to your internal url ['site/category', 'slug' => $slug, 'lang' => $lang] in your UrlRule's 'parseRequest' method. 'createUrl' is the other way round.

Yii2 -- Pretty Url -- Domain to Controller/Action with parameters

What will be the rules for my pretty url if I have the following scenario:
links like this where parameters may vary.
domain/?bt=<token>&e=<email>
or
domain/?lt=<token>&e=<email>
then should be processed in a controller/action. ie. mycontroller/get
Also, parameters should be accessible by $_GET inside the action.
the simplest way is based on the use of urlHelper
use yii\helpers\Url;
$myUrl = Url::to(['your_controller/your_action', 'bt' => 123, 'e' => 'myemail#gmail.com']);
Using the urlHelper function Url::to .. the url you need is properly formed depending of the urlManager configuration you have set in your config file
and the param a manager as show in the sample like entry in an array.
The post or get method is related to the type of metho you have in your ulr call if not other values are specified the url is formed as a get
and you can obtain the values you need in $_GET['bt'] and $_get['e']
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'call-backs/get',
'unsubscribes' => 'unsubscribes/get',
],
],
#scaisEdge, thank you for answering my question. maybe my question isn't that clear but this is the solution I made for my question after a hard find of clues and tips online.
All I wanted was that when a user clicks on a link, hitting the main page/main domain, it will go to my yii project (intended to be a webservice or an API like one) then will be handled by a precise controller and action.
'' => 'call-backs/get'
the code above answers the question. Cheers.

How to correctly specify migration namespace classes in yii2?

could someone explain me how can I correctly specify my modules migration namespaces? As I see in the documentation, it's:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
],
];
But there are no explanation in which file should I write the commands. I've tried it in config.php, as:
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\modules\adBoard\migrations',
],
But I don't know which controller class should I write. Could someone tell me in which file I have to specify it and how to specify it correctly?
If you refer to this documentation
Configuring Command Globally
Instead of entering the same option
values every time you run the migration command, you may configure it
once for all in the application configuration like shown below:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationTable' => 'backend_migration',
],
], ];
With the above configuration, each time you run the migration command, the backend_migration table will be used to record
the migration history. You no longer need to specify it via the
migrationTable command-line option.
Namespaced Migrations
Since 2.0.10 you can use namespaces for the
migration classes. You can specify the list of the migration
namespaces via migrationNamespaces. Using of the namespaces for
migration classes allows you usage of the several source locations for
the migrations. For example:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
], ];
this config should be placed in you console/config/main.php
but for namespaced migration remeber that starting for 2.0.10
depending on your yii2-template application (basic or advanced) the location of "console" specific settings are located in different directories.
For basic template, console fetch settings from <app>/config/console.php file.
And for advanced template, you should edit <app>/console/config/main.php file.
Remember that your settings for console will not affect web-settings, so if you want to register some component in the whole project, you have to duplicate it in both files.
P.S. I would like to add one more detail about advanced template, is that it has common setting for frontend and backend sub-apps, which is located in <app>/common/config/main.php, but those settings are not common with console commands.
I would like to share my experience with Yii2 namespaced migrations.
Scenario
I am using advanced template.
I have 100s of old migrations in console/migrations folder.
I have new extension which have namespaced migrations.
I have new migrations in old folder console/migrations.
I want to create future migrations with namespaces in new folder console/migrations/namespaced. I want to keep all old migrations intact which are located at console/migrations.
console/config/main.php configurations worked for me.
return [
'controllerMap' => [
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationNamespaces' => [
'console\migrations\namespaced',
'yii\swiftsmser\migrations'
]
],
],
//.... more configurations
];
With above configurations, when I executed yii migrate, It includes all above mentioned folders.
Note: To create new migration. Just make sure to use command like below.
yii migrate/create console\\migrations\\namespaced\\DLTTemplatesForSMS

Differentiate between Frontend and Backend Logs in DBTarget Yii2

I am logging into Db using existing Yii logging API.
But I want to differentiate between Frontend logs and Backend logs inside DB.
Everything that appears is common for both, I face difficulty tracing frontend logs.
Below is the image of DB Logs where GREEN marked are for backend logs, RED marked are for Frontend Logs.
You can use prefix property for this. This is callable that returns a string to be prefixed to every exported message with signature function ($message).
As default getMessagePrefix() is used there which prefixes the message with user IP, user ID and session ID.
You can use it to add there frontend and backend.
Thanks to #Bizley!
Inside both backend/config/main and frontend/config/main, I configured below; This is how my entire log configuration for Frontend looks like(Similarly you can do it for Backend);
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'prefix' => function ($message) {
return "[Frontend]";
},
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['error','info'],
],
],
],
Below is the view on UI for logs. With the Help of Prefix I can now easily differentiate between channels.

How to change the language based on user preference in Yii2?

my site start with a default language(which is English) then based on user's preference i should change it. is this possible in Yii2 ? is there any widget for this
I use contentNegotiator, without assign a language to the user the language is automatically assigned by the application.
for this
In config/main.php in bootstrap section start the component
'bootstrap' => [
'log',
'contentNegotiator',
],
in component section
'components' => [
'contentNegotiator' =>[
'class' => 'yii\filters\ContentNegotiator',
'languages' => [
'en-US',
'it-IT',
'fr-FR',
],
],
],
otherwise you can change when and where you want. Is application action eg you can do in any controller you chose. this way
\Yii::$app->language = 'zh-CN';