Unable to open Gii in Yii Framework on localhost - yii2

I am trying to open the gii module on my yii based project on localhost. But its not opening. I am using virtual hosts to access the project (http://pys.dev not http://localhost/pys.dev).
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'allowedIPs' => ['127.0.0.2'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}

How are you trying to access it? If you enabled pretty URLs then it should be http://domain/gii if not then http://domain/index.php?r=gii
Also check in your web/index.php that YII_ENV is set to dev

Related

Where do we configure Yii2 Queue extension in project?

I am trying to use the yii2-queue
https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/usage.md
It says:
In order to use the extension you have to configure it like the
following:
return [
'bootstrap' => [
'queue', // The component registers its own console commands
],
'components' => [
'queue' => [
'class' => \yii\queue\<driver>\Queue::class,
'as log' => \yii\queue\LogBehavior::class,
// Other driver options
],
],
];
My question is simple: In which PHP file, in which directory, should I put this code?
Note: I am using the Basic template.
For Yii2 Basic Template config/console.php
For Yii2 Advanced Template console/config/main.php
return [
'bootstrap' => [
'log',
'queue',
],
'components' => [
'queue' => [
'class' => \yii\queue\db\Queue::class,
'db' => 'db', // DB connection component or its config
'tableName' => '{{%queue}}', // Table name
'channel' => 'default', // Queue channel key
'mutex' => \yii\mutex\MysqlMutex::class, // Mutex that used to sync queries
'as log' => \yii\queue\LogBehavior::class,
// 'deleteReleased' => YII_ENV_PROD,
],
]
];
Refer Yii2 Queue extension guide
Add to the main.php file in backend or frond end you are using like this
'bootstrap' => ['log', 'queue'],
Add this to under component array
'queue' => [
'class' => Queue::class,
'db' => 'db', // DB connection component or its config
'tableName' => '{{%db_queue}}', // Table name
'channel' => 'default', // Queue channel key
'mutex' => MysqlMutex::class, // Mutex used to sync queries
]
To make it workfull you need to do same in console /config/main.php
file and run the command listen form documentaiton
It is very simple to configure it on yii2 basic, add the following configuration on config/web.php file, and for yii2 advanced if you are using frontend then add in frontend/config/main.php, if you are using backend then add to to backend/config.main.php.
Just like this
'components' => [
'request' => [
'cookieValidationKey' => 'htXdOInCiP6ut4gNbDO2',
'csrfParam' => '_frontendCSRF',
],
'queue' => [
'class' => \yii\queue\<driver>\Queue::class,
'as log' => \yii\queue\LogBehavior::class,
// Other driver options
],
]

How to create and use environment variable .env yii2

I want to create a file .env:
FACEBOOK_CLIENT_ID=*****
FACEBOOK_CLIENT_SECRET=*****
and use variable for config
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => env('FACEBOOK_CLIENT_ID'),
'clientSecret' => env('FACEBOOK_CLIENT_SECRET'),
],
I have found the solution.
I use package vlucas/phpdotenv.
Thank for all
You can archive this by Utilizing the enviroment constants. If you are using Yii2 advanced when you initialize your application as dev or production yii sets a constant YII_ENV as either dev or production in your index.php entry script.
If you are using yii basic you can set it as per your enviroment. For example we want to define config for dev.
We will proceed and edit our /web/index.php to
defined('YII_ENV') or define('YII_ENV', 'dev');
Then in our config file we would have the following
'facebook' => [
'class' => 'dektrium\user\clients\Facebook',
'clientId' => (YII_ENV_DEV ? 'Your key when in developent' : 'Your Key if not in developement'),
'clientSecret' => (YII_ENV_DEV ? 'Your key when in developent' : 'Your Key if not in developement'),
],
Refere to this for more details on Enviroment Constants http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html#environment-constants
There's a package for Yii2 you can use for dotenv
https://github.com/yiithings/yii2-dotenv
Install:
composer require --prefer-dist yiithings/yii2-dotenv "*"
Usage:
Create a .env in project root directory
DB_HOST="localhost"
DB_NAME="yii2basic"
DB_USER="root"
DB_PASS="YourPassword"
config/db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host='. env('DB_HOST') .';dbname='. env('DB_NAME'),
'username' => env('DB_USER'),
'password' => env('DB_PASS'),
'charset' => 'utf8',
];
You could use the dotenv package offered by Symfony. https://symfony.com/doc/current/components/dotenv.html
using it in a PHP file.
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
// You can also load several files
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
.env file
DB_USER=root
DB_PASS=pass
accessing the variables from the .env file
$dbUser = $_ENV['DB_USER'];
// you can also use ``$_SERVER``

Yii2 asset compression using closure compiler and yuicompresser

http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
i generate assets.php in the root of the application using this command yii asset/template assets.php
assets.php
<?php
/**
* Configuration file for the "yii asset" console command.
*/
// In the console environment, some path aliases may not exist. Please define these:
// Yii::setAlias('#webroot', __DIR__ . '/../web');
// Yii::setAlias('#web', '/');
return [
// Adjust command/callback for JavaScript files compressing:
'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
// Adjust command/callback for CSS files compressing:
'cssCompressor' => 'java -jar yuicompressor.jar --type css {from} -o {to}',
// The list of asset bundles to compress:
'bundles' => [
// 'app\assets\AppAsset',
// 'yii\web\YiiAsset',
// 'yii\web\JqueryAsset',
],
// Asset bundle for compression output:
'targets' => [
'all' => [
'class' => 'yii\web\AssetBundle',
'basePath' => 'C:/wamp/www/flywings/frontend/web/assets',
'baseUrl' => '/flywings/frontend/web/assets',
'js' => 'js/all-{hash}.js',
'css' => 'css/all-{hash}.css',
],
],
// Asset manager configuration:
'assetManager' => [
//'basePath' => '#webroot/assets',
//'baseUrl' => '#web/assets',
],
];
and i installed closure compiler and yuicompresser in my pc at the following path C:\closure-compiler and C:\YUI\yuicompressor\build
and i dont know how to proceed to next step.in the assets.php whats the {from} {to} and how to add the bundles there(manually ?).I am using the advanced application.

Why Yii2 module separate configuration does not work in basic app?

I have a yii2 basic application with 2 parts (web and service for mobile).
I have created a module to handle the restful requests fired from mobile . I want to configure this module to be rest. So I created a config file for this module in side the module directory. as mentioned in the yii2 documentation for modules
/config/config.php:
return [
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'rules' => array(
[
'class' => 'yii\rest\UrlRule',
'controller' => 'mobile/mobile-clients',
'extraPatterns' => ['GET search' => 'search']
],
),
],
'request' => [
'class' => '\yii\web\Request',
'enableCookieValidation' => false,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
]
];
the module class is as follows:
<?php
namespace app\modules\Mobile;
use Yii;
use yii\base\Module;
class MobileService extends Module {
public $controllerNamespace = 'app\modules\Mobile\controllers';
public function init() {
parent::init();
Yii::configure($this, require(__DIR__ .DIRECTORY_SEPARATOR
.'config'.DIRECTORY_SEPARATOR .'config.php'));
}
}
The problem is that the request component is not working as expected while it works fine when configured in the application configuration (config/main.php)
same for the urlManager.
Any Ideas?
The Solution to my problem is to create api application that is a new application inside the yii2 basic app. It shares the models and the vendors directory but has its own configuration and entry script (index.php). This is the solution link for more information .
EDIT:
Do not forget to add the user component in the api.config file
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => false,
],
I think using yii2 advanced application structure is better for a case like mine. But this solution works perfect :) .
Best.

Yii 2.0 enabling remote access to gii

Simple question, trying to enable remote access to gii in yii 2 - docs say http://www.yiiframework.com/doc-2.0/guide-start-gii.html
Note: If you are accessing Gii from a machine other than localhost, the access will be denied by default for security purpose. You can configure Gii to add the allowed IP addresses as follows,
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'] // adjust this to your needs
],
Thing is, it doesn't say where to add this - guesing config/web.php
But under what section?
2 places you need to add this.
Usually it is done like this in your main-local.php
if (!YII_ENV_TEST) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
So you need to add gii in the bootstrap section of the config and in the modules section. This will turn basically append them to the array from your config/main.php
return [
'id' => 'app-backend',
'basePath' => dirname(DIR),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
],
],
On the link you gave, take a look above. You should do:
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'] // adjust this to your needs
];
}