how to access variables in main.php into another php file - php

I'm very new to php web development with yii2 framework. I am working on a web application and I have few variables in my return of main.php file (config folder).
return [
'components' => [
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
//some fields
],
'google' => [
'class' => 'yii\authclient\clients\GoogleOAuth',
'clientId' => 'somerandomnumber.apps.googleusercontent.com',
'clientSecret' => '6f7g8h9i10j',
],
],
],
]
I have to use google's clientId in Model folder's php file.
<a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2FCURRENT-DOMAIN%2Fgoogle%2Dlogin&response_type=permission&scope=email+profile+openid&client_id=GET THE CLIENT ID HERE">
<div class="gplus_seperator">
<div class="social_logo">
<span class="icon-font">p</span>
</div>
<div class="social_text">Sign in with Google+</div>
</div>
</a>
Can some one help me. TIA

You can access components using:
Yii::$app->components['authClientCollection']['clients']['go‌​ogle']['clientId'];

Related

Using krivochenko/yii2-cropper to upload image doesn't work

I don't know what's wrong with my code because i think, i have following all the step from github krivochenko/yii2-cropper But button to cropping or delete upload doesn't work and and also the css displayed on the form is not neat like this :
i'm not see any error in console, and i want to include the code i made :
this is in view :
use budyaga\cropper\Widget;
<div class="form-group col-sm-8 col-md-8 col-lg-6">
<?= $form->field($model, 'imageFile')->widget(Widget::className(), [
'uploadUrl' => Url::toRoute($uploadPhoto['url']),
]) ?>
</div>
then this is in controller:
return $this->render('input', [
'model' => $model,
'uploadPhoto' => [
'class' => 'budyaga\cropper\actions\UploadAction',
'url' => 'content/user/create',
'path' => $this->baseApp.'/files/uploads',
]
]);
So what's wrong with this code?
finally, i can understand what wrong with my code.
I need to adjust css from this widget in my own with add param in widget input form.:
<?= $form->field($model, 'imageFile')->widget(Widget::className(), [
'uploadUrl' => Url::toRoute($uploadPhoto['url']),
'cropAreaHeight' => 'auto' //here
]) ?>
i put Upload Photo index into create action, Upload Photo should be placed in function action(general) like this:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'uploadPhoto' => [
'class' => 'budyaga\cropper\actions\UploadAction',
'url' => 'dir_to_save_the_result_of_crop',
'path' => 'path_to_save_the_result_of_crop',
]
];
}
And this widget krivochenko/yii2-cropper can works also really easy to use in yii2

Twig not working in Yii2

I want to use Twig in Yii2 framework but it isn't working.
I am using yii2-app-advanced as the base project but I am new in the Yii world so I think I am not configuring Twig in the right way.
First I downloaded it using:
composer require yiisoft/yii2-twig
Then I follow this instructions but it's not easy to understand:
https://github.com/yiisoft/yii2-twig/blob/HEAD/docs/guide/installation.md#configuring-application
It says:
In order to start using Twig you need to configure view component like the following:
[
'components' => [
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => [
'html' => ['class' => '\yii\helpers\Html'],
],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
]
In which file I have to paste this code?
In my index.php file I added the following code but it is not working:
{% if true %}
<p>It is true.</p>
{% else %}
<p>It is false.</p>
{% endif %}
I solved it doing this:
I modified the file backend/config/main-local-php:
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'CPeTotdTU98geIyM7q0PljmCpJbupPN4',
],
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => [
'html' => ['class' => '\yii\helpers\Html'],
],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
The SiteController.php file has the actionIndex() function. I added the extension .twig:
public function actionIndex()
{
return $this->render('index.twig');
}
And then I modified the name of the file backend/views/sire/index.php to index.twig.

Unknown component ID: authClientCollection

I'm doing my site like this tutorial, https://www.mushtaqtahir.com/blog/2/facebook-authentication-using-yii2-authclient, but when I'm trying to go to my localhost/basic/web/index.php I have that error:
Invalid Configuration – yii\base\InvalidConfigException
Unknown component ID: authClientCollection
in basic\vendor\yiisoft\yii2\di\ServiceLocator.php.
Maybe it's a problem in my config. I haven't in my config folder main.php file and I create it and add data like in tutorial.
Check in you basic/config/web.php and add in component section the requiredc params
'components' => [
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId' => 'YOUR APP CLIENT ID',
'clientSecret' => 'YOUR APP CLIENT SECRET',
'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
],
],
],
.....
],

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 Moving folder mdm-admin of mdmsoft and xml view appeared

I'm using Yii2 to develop my web app. But when I using yii2-admin of mdmsoft with version 3 for prettyURL, I move folder of yii2-admin to modules folder, and change main.php like this:
'bootstrap' => [
'admin'
],
'authManager' => [
'class' => 'yii\rbac\DbManager', // or use 'yii\rbac\DbManager'
],
'modules' => [
'rbac' => [
'class' => 'app\modules\rbac\Module',
'layout' => '#backend/views/layouts/admin',
'controllerMap' => [
'assignment' => [
'class' => 'app\modules\rbac\controllers\AssignmentController',
]
],
]
],
],
'as access' => [
'class' => 'mdm\admin\classes\AccessControl',
'allowActions' => [
'*',
]
],
But when I access mydomain/rbac/assignment or mydomain.com/rbac/route ,... it's just a xml view, nothing more?
How I can fix it?

Categories