Asset bundle generate empty JavaScript and CSS files
namespace frontend\assets;
return [
'bundles' => [
'frontend\assets\AppAsset',
],
'targets' => [
'frontend\assets\AppAsset' => [
'basePath' => 'e:/path/yii2.loc/www',
'baseUrl' => '',
'js' => 'js/{ts}.js',
'css' => 'css/{ts}.css',
],
],
'assetManager' => [
'basePath' => 'e:/path/yii2.loc/www/assets',
'baseUrl' => '',
],
];
config.php
return [
'bundles' => [
'frontend\assets\AppAsset',
],
'targets' => [
'frontend\assets\AppAsset' => [
'basePath' => 'e:/path/yii2.loc/www',
'baseUrl' => '',
'js' => 'cache/{ts}.js',
'css' => 'cache/{ts}.css',
],
],
'assetManager' => [
'basePath' => 'e:/path/yii2.loc/www/assets',
'baseUrl' => '',
],
];
Then in console
yii asset e:\path\config.php e:\path\compressed.php
//compresed.php it's result file with name of compressed files
And in config
'assetManager' => [
'bundles' => require dirname(__DIR__) . '/assets/compressed.php',
],
CSS and JavaScript files are in a directory:
e:/path/yii2.loc/www/css
And
e:/path/yii2.loc/www/js
Bundle generate empties to:
e:/path/yii2.loc/www/cache/css and e:/path/yii2.loc/www/cache/js
What did I do wrong?
Inside your config.php, try to config component 'assetManager' like as the following LOCs:
'components' => [
'assetManager' => [
'class' => 'yii\web\AssetManager',
'basePath' => 'YOUR_BASE_PATH'
],
],
You should set aliases #web and #webroot, because of this file will be use in console script. Then use aliases for setting basePath and baseUrl parameters
Check out for more info https://www.yiiframework.com/doc/guide/2.0/en/structure-assets
Related
Let me tell you the case.
Basically I have separate backend in yii2 advanced template.
Why ? This is the reason
My office have a lot of branch office in a country with a lot of departements of each branch.
This departements, I have interpretation of them as modules.
The departement name is same but sometime, they have a lot of different
behaviours.
As example admin in headquarters can erase employee name in branch
office, but admin branch office , they can not.
So, I choose to separate them into backend folder each like this :
backend (which is portal branch and also super-admin backend)
-modules
-human_resource
backend-jkt (which is Jakarta Indonesia backend)
-modules
-human_resource
My question is :
When user successfully login to backend, then i created a link to backend-jkt, it's automatically login also.
As vice versa,
When people directly to backend-jkt but not logged in to backend, it's automatically redirect to backend's login,
Now my situation is: when user logged in to backend, then click link "Jakarta" as above in image, user have to sign in again.
This is my config in backend
<?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-backend',
'name' => 'Backend System',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
'mimin' => [
'class' => '\hscstudio\mimin\Module',
],
'SuperAdmin' => [
'class' => 'backend\modules\super_admin\SuperAdmin',
],
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity-backend',
'httpOnly' => true
],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
'savePath' => sys_get_temp_dir(),
],
'request' => [
'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
'csrfParam' => '_csrf-backend',
],
'assetManager' => [
'bundles' => [
'dmstr\web\AdminLteAsset' => [
],
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'suffix' => '.html',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'urlManagerBackendJkt' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/backend-jkt/web/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'http://jkt.tresnamuda.local/' => '#app/index',
],
],
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
'as access' => [
'class' => '\hscstudio\mimin\components\AccessControl',
'allowActions' => [
// add wildcard allowed action here!
'site/*',
'debug/*',
// 'mimin/*', // only in dev mode
],
],
'params' => $params,
];
And this is the backend-jkt
<?php
$params = array_merge(
require __DIR__ . '/../../backend/config/params.php',
require __DIR__ . '/../../backend/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-backend_jkt',
'name' => 'Jkt Backend System',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend_jkt\controllers',
'bootstrap' => ['log'],
'modules' => [
'mimin' => [
'class' => '\hscstudio\mimin\Module',
],
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity-backend',
'httpOnly' => true
],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
'savePath' => sys_get_temp_dir(),
],
'request' => [
'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
'csrfParam' => '_csrf-backend',
],
'assetManager' => [
'bundles' => [
'dmstr\web\AdminLteAsset' => [
],
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'suffix' => '.html',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
'as access' => [
'class' => '\hscstudio\mimin\components\AccessControl',
'allowActions' => [
// add wildcard allowed action here!
'site/*',
'debug/*',
// 'mimin/*', // only in dev mode
],
],
'params' => $params,
];
your question about cookies that place in user's browsers seprate by domain and Path , so you have to store it for next domain Path , I recommend to you after clicking Jakarta send user-id and private-key to Jakarta and there force login that user-id by simple command :
if(private-key is Okey and you get $user-id by POST ) {
$user = User::findOne($user-id);
Yii::$app->getUser()->login($user);
}
private-key is simple or advance why that you can increase your security , you may leave it and just check have user-id or not !
I want to know how the Yii framework send the request to the right controller depending on the subdomain passed in the URL:
www.mysystem.com -> This request is handled by the default controller of a specific module in my system.
But when the user come to access his store, he will use the URL: storename.mysystem.com. (There are many different store names)
I would like to know where in Yii can I find the config to set wich module/controller will handle this request.
Thank you.
Here is my config for Yii2
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')
);
$host = (!empty($_SERVER['HTTP_HOST']))?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
$segments = explode('.',$host);
defined('SUBDOMAIN') or define('SUBDOMAIN', strtolower($segments[0]));
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
SUBDOMAIN => [
'class' => 'backend\modules\\'.SUBDOMAIN.'\\'.ucfirst(SUBDOMAIN),
],
],
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'db' => [
'tablePrefix' => SUBDOMAIN.'_',
],
'urlManager'=>[
'rules' => [
'<controller:[\w-]+>/<id:\d+>' => ''.SUBDOMAIN.'/<controller>/view',
'<controller:[\w-]+/<action:[\w-]+>' => ''.SUBDOMAIN.'/<controller>/<action>',
'<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => ''.SUBDOMAIN.'/<controller>/<action>',
],
],
],
'params' => $params,
];
I have installed new yii2. Installed dektrium/yii2-rbac. Updated my database schema by migration. I don't have authManager component configured. My configuration file:
'components' => [
'request' => [
'cookieValidationKey' => 'asdasd123456qwerty',
],
...
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
...
'db' => require(__DIR__ . '/db-local.php'),
'urlManager' => [...],
],
'params' => $params,
'modules' => [
'rbac' => 'dektrium\rbac\RbacWebModule',
],
When I try get localhost/basic/web/rbac (default admin/admin), I get 403 Forbidden. Where is the mistake?
'modules' => [
'rbac' => [
'class' => 'dektrium\rbac\RbacWebModule',
'admins' => ['admin'],
]
],
Here is an answer!
I think you need to configure authManager in your configuration file
'components' => [
...
'authManager' => [
'class' => 'dektrium\rbac\components\DbManager',
],
...
],
...
I just uploaded my yii advanced project to my centos server, but I can't seem to get past the migrate phase. When I try to run yii migrate the following error occurred:
`Setting unknown property: yii\console\ErrorHandler::errorAction'
I have no idea why this happens, because it works fine when I run it locally on my windows computer.
My yii advance project is bit different than a normal Yii advanced. The backend has been separated from the frontend so it just contains the console and frontend directory.
common/config/main.php
$config = require(__DIR__ . '/main-console.php');
array_push($config['bootstrap'], 'site');
$config['components']['errorHandler'] = [
'errorAction' => 'site/error',
];
$config['components']['user'] = [
'identityClass' => 'frontend\models\User',
'enableAutoLogin' => true,
];
$config['components']['session'] = [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
];
$config['components']['request'] = [
'cookieValidationKey' => 'IBzCJMjLWUaXMZemYUej',
'csrfParam' => '_frontendCSRF',
];
$config['components']['site'] = [
'class' => 'frontend\components\SiteComponent',
];
return $config;
main-console.php
$params = array_merge(
require(__DIR__ . '/params.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log','debug'],
'sourceLanguage' => 'en-US',
'controllerNamespace' => 'frontend\controllers',
'aliases' => [
'#local_media' => '#frontend/web/uploads/media',
],
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#frontend/messages',
],
],
],
'assetManager' => [
'bundles' => false,
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'trace'],
],
],
],
'defaultRoute' => 'site/view',
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'enableStrictParsing' => false,
'rules' => require('routes.php'),
],
],
'params' => $params,
];
Can someone give me some advies on how to solve this problem?
You problem is that you specify error action into common/config/main.php. Error action must be used only with web apps, not console. So move this to your frontend and backend configs separately:
$config['components']['errorHandler'] = [
'errorAction' => 'site/error',
];
There is no errorAction attribute in yii\console\ErrorHandler class. There is one in yii\web\ErrorHandler though. I'm not sure why this works on your local machine because it shouldn't. I guess some other configuration is in place there.
I'm running Yii2 and I have up until recently been working within a single application; however I needed to start adding an admin area and I didn't start with the advanced application template, just the basic one, but still move some directories around then and everything worked fine.
Now I have a directory structure like this...
admin/
system/
controllers/
models/
views/
index.php
common/
vendor/
.bowerrc
composer.json
composer.lock
yii
yii.bat
console/
css/
images/
js/
system/
controllers/
models/
views/
index.php
So as you can see, the root dir acts as the web dir and the system/ dir acts as the application dir for the frontend.
The frontend of the site works absolutely fine, but I am having issues with the admin section.
Within the admin dir, the base dir acts as the web directory the admin/system/ acts as the application directory.
This is the error I get when attempting to access: admin/staff/login
Invalid Parameter – yii\base\InvalidParamException
The file or directory to be published does not exist: common\vendor\bower/jquery/dist
This is the contents of the admin/index.php file:
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../common/vendor/autoload.php');
require(__DIR__ . '/../common/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../common/config/constants.php');
$config = require(__DIR__ . '/system/config/main.php');
(new yii\web\Application($config))->run();
Here are the contents of the admin/system/config/main.php file:
<?php
$params = require(__DIR__ . '/../../../common/config/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'version' => '0.1',
'vendorPath' => 'common\vendor',
'defaultRoute' => 'site/index',
'bootstrap' => [
'log',
'common\base\Settings',
],
'components' => [
'request' => [
'enableCookieValidation' => false,
'enableCsrfCookie' => false,
'csrfParam' => '_admin_csrf',
],
'view' => [
'theme' => [
// This data is setup dynamically via the bootstrapping process
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\Staff',
'enableAutoLogin' => false,
'loginUrl' => 'staff/login',
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'session' => [
'name' => 'PHPADMINSESSID',
],
'formatter' => [
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
// The below viewPath only acts as the base dir and will be changed during the bootstrap process to append the correct locale dir to it
'viewPath' => '#common/mail',
// These are relative to the final value of viewPath
'htmlLayout' => 'layouts/default-html',
'textLayout' => 'layouts/default-text',
],
'authManager' => [
'class' => 'app\components\AuthManager',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error'],
'logFile' => '#app/runtime/logs/errors.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['warning'],
'logFile' => '#app/runtime/logs/warnings.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'logFile' => '#app/runtime/logs/info.log',
'enabled' => false,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['trace'],
'logFile' => '#app/runtime/logs/trace.log',
'enabled' => false,
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
],
],
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapAsset' => [
'css' => [],
],
],
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#common/languages',
//'on missingTranslation' => ['common\components\TranslationEventHandler', 'handleMissingTranslation'],
],
],
],
'db' => require(__DIR__ . '/../../../common/config/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'allowedIPs' => ['127.0.0.1','::1']
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1','::1']
];
}
return $config;
I have deleted by common/vendor dir and ran composer install again but no change.
Do I have some paths wrong somewhere or what is the problem?
I think the path could be
require(__DIR__ . '/../../common/vendor/autoload.php');
require(__DIR__ . /..'/../common/vendor/yiisoft/yii2/Yii.php');
....
This could create problems for components called inside other or for which you have not set a specific path (eg jquery). I believe that the cause might be this and then to get around this problem you restore the directory vendors in its default configuration or identify the components that must be loaded with the appropriate path.
Problem was this line in the admin config file:
'vendorPath' => 'common\vendor',
common does not act as an alias there and I needed to change it to:
'vendorPath' => '..\common\vendor',
..to correctly reference the common directory.