Find us on facebook

Showing posts with label yii2. Show all posts
Showing posts with label yii2. Show all posts

Jun 21, 2015

server API calls with AngularJS - yii2 (Part 2)

assets/AppAsset.php


web/js/services.js


web/js/controllers.js


Angular JS with Yii2

assets/AppAsset.php


assets/AngularAsset.php


web/js/app.js


web/js/controllers.js

view file


Jun 17, 2015

Send emails using SMTP Yii 2.0

In your controller
Yii::$app->mail->compose()->setFrom('yyy@gmail.com')->setTo('sss@gmail.com')->setSubject('Email sent from Yii2-Swiftmailer')->send();

In common/config/main-local.php
 In components array
 'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',  // e.g. smtp.mandrillapp.com or smtp.gmail.com
        'username' => 'ttt@gmail.com',
        'password' => 'ggg',
        'port' => '587', // Port 25 is a very common port too
        'encryption' => 'tls', // It is often used, check your provider or mail server specs
        ],
        ],

Jun 13, 2015

Enable Pretty URL - Yii2 Advanced template

Step 1 : edit common/config/main.php as below
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
Step 2 : Add following .htaccess file into backend/web folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Step 3 :Add following .htaccess file into frontend/web folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Step 4: Add following to apache\apache2.4.9\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:\Projects\new\yii-application"
ServerName yii2_test.ml
<Directory "D:\Projects\new\yii-application">
        AllowOverride All
    </Directory>
</VirtualHost>

Step 5: Enable mod_rewrite on apache\apache2.4.9\conf\httpd.conf


Configuring RBAC Manager Yii2-Advanced Template

Step 1 : edit common/config/main.php And console/config/main.php  as below
'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],


Step 2: use the migration stored in @yii/rbac/migrations:

[yii migrate --migrationPath=@yii/rbac/migrations]

Step 3: Install yii2-admin via composer [composer.phar should be within yii-application folder]

1) php composer.phar require mdmsoft/yii2-admin "~2.0" OR
2) add "mdmsoft/yii2-admin": "~2.0" to the require section of your composer.json file[and run composer.phar update].


 Step 4: edit common/config/main.php as below

'modules' => [
        'admin' => [
            'class' => 'mdm\admin\Module',
        ]
],
'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'admin/*', // add or remove allowed actions to this list
        ]
    ],

Now you can access admin panel for creating/editing Roles/Permissions , Assign users to roles etc...
http://localhost/path/to/index.php?r=admin
http://localhost/path/to/index.php?r=admin/route
http://localhost/path/to/index.php?r=admin/permission
http://localhost/path/to/index.php?r=admin/menu
http://localhost/path/to/index.php?r=admin/role
http://localhost/path/to/index.php?r=admin/assignment

Install Yii2 Advanced Application Template via Composer

Step 1 : Create a folder under your web root (Let's say yii2)
Step 2 : Copy the composer.phar file into to yii2
Step 3 : Go to that folder in cmd
Step 4 : Type in cmd [php composer.phar global require "fxp/composer-asset-plugin:1.0.0"]

Step 5 : Type in cmd [php composer.phar create-project --prefer-dist yiisoft/yii2-app-advanced yii-application]
Step 6 : In middle of the process this would ask for a token.

(To get a token, login to your github profile and generate a token. Steps are as below.
Step A : In the top right corner of any page, click your profile photo, then click Settings.

Step B : Personal access tokensIn the user settings sidebar, click Personal access tokens.


Step C : Generate new token buttonClick Generate new token.


Step D : Token description field Give your token a descriptive name.
Step E:  Selecting token scopesSelect the scopes you wish to grant to this token. The default scopes allow you to interact with public and private repositories, user data.
Step F: Generate token buttonClick Generate token.


[Newly created tokenCopy the token to your clipboard. For security reasons, after you navigate off this page, no one will be able to see the token again.]
)
Step 6 :  Copy and paste this token into cmd and hit enter.

Step 7 :  It says token stored successfully. And install all the packages.


Step 8 : Move to the folder that is project created and Run in cmd [php init]

Step 9 : Create a new database in mysql and adjust the components['db'] configuration in common/config/main-local.php accordingly.


Step 10 : Apply migrations with console command yii migrate.

Step 11 : Open yii2-advanced web on browser





Jun 8, 2015

you should configure authmanager option error (When using yii migrate --migrationPath=@yii/rbac/migrations)

yii2-base template: change config/console.php configuration file where the authManager needs to be declared. And change the config/web.php too.
'authManager' => [
    'class' => 'yii\rbac\DbManager',
],

yii2-advanced template: Change console/config/main.php And common/config/main.php

Then run yii migrate --migrationPath=@yii/rbac/migrations command

Jun 6, 2015

Yii2 - Basic - Create new project

php composer.phar global require "fxp/composer-asset-plugin:1.0.0"


php composer.phar create-project --prefer-dist yiisoft/yii2-app-basic D:/wamp/www/angular/skwirk


Feb 25, 2015

Yii2 basic commands

Save data
$member = new Member;
$member->name = 'John';
$member->email = 'john@gmail.com';
$member->save();  // equivalent to $member->insert();

Select *
$model = $connection->createCommand('SELECT * FROM member');
$members = $model->queryAll();

Select One row
$model = $connection->createCommand("SELECT * FROM member where member_id=$id");
$member = $model->queryOne();

Query Column
$model = $connection->createCommand('SELECT name FROM member');
$members = $model->queryColumn();

JSON format return
$items = ['book1', 'book2', 'book3', 'book4'];
\Yii::$app->response->format = 'json';
return $items;

OR

$items = ['book1', 'book2', 'book3', 'book4'];
return \yii\helpers\Json::encode($items);