Tuesday, May 7, 2019

Htaccess Configuration

root .htaccess
==============
Options -Indexes

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} /(uploads)
RewriteRule ^uploads/(.*)$ uploads/$1 [L]

# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]

# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^backend(/(.*))?$ backend/web/$2 [NC,L]

# handle the case of frontend
RewriteRule .* frontend/web/$0

RewriteRule ^api(/(.*))?$ api/$3

</IfModule>

# Deny accessing below extensions
#<Files ~ "(.json|.lock|.git)">
#Order allow,deny
#Deny from all
#</Files>

# Deny accessing dot files
#RewriteRule (^\.|/\.) - [F]


Frontend/Web .htaccess
======================
RewriteEngine on

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

Frontend/Config main
=========================
use \yii\web\Request;

$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-frontend',
   'basePath' => dirname(__DIR__),
   'bootstrap' => ['log'],
   'controllerNamespace' => 'frontend\controllers',
   'components' => [
       'request' => [
           'csrfParam' => '_csrf-carco-frontend',
           'baseUrl' => str_replace('/frontend/web', '', (new Request)->getBaseUrl()),
       ],
       'user' => [
           'identityClass' => 'frontend\models\MyUser',
           'enableAutoLogin' => true,
           'identityCookie' => ['name' => '_identity-trending-frontend', 'httpOnly' => true],
       ],
       'session' => [
           // this is the name of the session cookie used for login on the frontend
           'name' => 'advanced-trending-frontend',
       ],
       'log' => [
           'traceLevel' => YII_DEBUG ? 3 : 0,
           'targets' => [
               [
                   'class' => 'yii\log\FileTarget',
                   'levels' => ['error', 'warning'],
               ],
           ],
       ],
       'errorHandler' => [
           'errorAction' => 'site/error',
       ],
       'urlManager' => [
           'enablePrettyUrl' => true,
           'showScriptName' => false,
           //'enableStrictParsing' => true,
           'rules' => [
               '/' => 'site/index',
               'reset-password' => 'site/reset-password',
               'logout' => 'site/logout',
               '<controller:\w+>/<id:\d+>' => '<controller>/view',
               '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
               '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
           ],
       ],
   ],
   'params' => $params,
];

Backend/Web .htaccess
======================
RewriteEngine on

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

Backend/Config main
=====================

$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'
);

use \yii\web\Request;

$baseUrl = str_replace('/backend/web', '/backend', (new Request)->getBaseUrl());
return [
   'id' => 'app-backend',
   'basePath' => dirname(__DIR__),
   'controllerNamespace' => 'backend\controllers',
   'bootstrap' => ['log'],
   'modules' => [
       'gridview' => [
           'class' => '\kartik\grid\Module'
       ]
   ],
   'components' => [
       'assetManager' => [
           'class' => 'yii\web\AssetManager',
           'forceCopy' => true,
       ],
       'request' => [
           'baseUrl' => $baseUrl,
       ],
       'user' => [
           'identityClass' => 'common\models\User',
           'enableAutoLogin' => true,
           //'authTimeout' => 172800, // 48 hour
           'identityCookie' => ['name' => '_identity-trending-backend', 'httpOnly' => true],
       ],
       'session' => [
// this is the name of the session cookie used for login on the backend
           'name' => 'trending-backend',
       //'savePath' => sys_get_temp_dir(),
       ],
       'log' => [
           'traceLevel' => YII_DEBUG ? 3 : 0,
           'targets' => [
               [
                   'class' => 'yii\log\FileTarget',
                   'levels' => ['error', 'warning'],
               ],
           ],
       ],
       'errorHandler' => [
           'errorAction' => 'site/error',
       ],
       'urlManager' => [
           'enablePrettyUrl' => true,
           'showScriptName' => false,
           //'enableStrictParsing' => true,
           'baseUrl' => $baseUrl,
           'rules' => [
               '/' => 'site/index',
               'logout' => 'site/logout',
               // users
               'users' => 'users/index',
               // tips
               'tips' => 'tips/index',
               'reports' => 'reports/index',
               'mails' => 'mail-content/create',
               'withdraw' => 'withdraw/index',
               '<controller:\w+>/<id:\d+>' => '<controller>/view',
               '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
               '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
           ],
       ],
   ],
   'params' => $params,
];

Controller Search Search Controller Query

Main Controller
===============
$query = TableStudent::find()
                    ->select('table_student.*, table_state.name AS state_name, table_state.id AS state_id, table_country.name AS country_name, table_country.id AS country_id')
                    ->innerJoin('table_state', 'table_state.id = table_student.state_id')
                    ->innerJoin('table_country', 'table_country.id = table_state.country_id')
                    ->where(['=', 'table_student.id', $id])
                    //->asArray()
                    //->all();
                    ->one();
       
        // add conditions that should always apply here
return $query;

Search Controller
=================
public function search($params)
    {
        //$query = TableStudent::find();

        /* $query = new Query;
        $query ->select([
                'table_student.id AS id',
                'table_student.name AS name',
                'table_student.email AS email',
                'table_student.address AS address',
                'table_state.name AS state_name',
                'table_country.name AS country_name',
                ]) 
                ->from('table_student')
                ->join( 'INNER JOIN', 'table_state','table_state.id = table_student.state_id')
                ->join( 'INNER JOIN', 'table_country','table_country.id = table_state.country_id'); */
       
        $query = TableStudent::find()
                    ->select('table_student.*, table_state.name AS state_name, table_country.name AS country_name')
                    ->innerJoin('table_state', 'table_state.id = table_student.state_id')
                    ->innerJoin('table_country', 'table_country.id = table_state.country_id');
       
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

         $dataProvider->sort->attributes['country_name'] = [
            'asc' => ['table_country.name' => SORT_ASC],
            'desc' => ['table_country.name' => SORT_DESC],
        ];
       
        $dataProvider->sort->attributes['state_name'] = [
            'asc' => ['table_state.name' => SORT_ASC],
            'desc' => ['table_state.name' => SORT_DESC],
        ];
       
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'state_id' => $this->state_id,
        ]);

        $query->andFilterWhere(['like', 'table_student.name', $this->name])
            ->andFilterWhere(['like', 'table_student.email', $this->email])
            ->andFilterWhere(['like', 'table_student.address', $this->address])
            ->andFilterWhere(['like', 'table_country.name', $this->country_name])
            ->andFilterWhere(['like', 'table_state.name', $this->state_name]);

        return $dataProvider;
    }