Skip to content

Instantly share code, notes, and snippets.

@Faryshta
Last active April 14, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Faryshta/f8490e6556fb0153b90158dad02433f8 to your computer and use it in GitHub Desktop.
Save Faryshta/f8490e6556fb0153b90158dad02433f8 to your computer and use it in GitHub Desktop.
Yii2 Application Excercise

Instructions for the Yii2 Application Excercise

Fork Advanced Application

Login or Signin on https://github.com/

Enter to https://github.com/yiisoft/yii2-app-advanced

Click the Fork button

Fork Button

And it will generate you your own repostitory that you can edit and use without restrictions.

clone this new repository

git clone git@github.com:YOUR_USERNAME/yii2-app-advanced.git

Application api

Create a folder in the project root named api

mkdir api

Create the following files

api/config/.gitignore

main-local.php
params-local.php

api/config/bootstrap.php

<?php
// configuración al iniciar el app, viene vacia por defecto

api/config/main.php

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-api', // !important to not repeat ids on app
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
    ],
    'params' => $params,
];

api/config/params.php

<?php
return [
];

api/runtime/.gitignore

*
!.gitignore

api/web/assets/.gitignore

*
!.gitignore

api/web/.gitignore

/index.php
/index-test.php

Environments

The previous step created an skeleton for the api application. Now we create the environmental files to be used for the init command.

Ahora hay que hacerla instalable creando los archivos de ambiente de trabajo o environments

environments/prod/api/config/main-local.php

<?php
return [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '',
        ],
    ],
];

environments/dev/api/config/main-local.php

<?php

$config = [];

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;

environments/prod/api/config/params-local.php

<?php
return [
];

environments/dev/api/config/params-local.php

<?php
return [
];

environments/dev/api/web/index-test.php

<?php

// NOTE: Make sure this file is not accessible when deployed to production
if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
    die('You are not allowed to access this file.');
}

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = require(__DIR__ . '/../../tests/codeception/config/neogcio/acceptance.php');

(new yii\web\Application($config))->run();

environments/prod/api/web/index.php

<?php
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

environments/dev/api/web/index.php

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

Finally we edit the file boostrap.php file and add a line

common/config/bootstrap.php

Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');

Composer

Execute the composer commands for instalation

composer self-update
composer global require "fxp/composer-asset-plugin:~1.1.3"
composer global update
composer create-project --prefer-dist
./init

Controller

api/controllers/SiteController.php

<?php

namespace api\controllers;

use Yii;

class SiteController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return __FILE__;
    }
}

Test

Deploy a server on the api/web with the php -S command and check it on the browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment