backend/config/main.php ->assetManager -> baseUrl - php

I'm following the method described here to have my control panel in backend folder.
The problem is I don't want to create two assets folders so I want to set assetManager baseUrl property in my backend/config/main.php but I'm enable to get the URL of the backend in the config. I don't want it hard coded because I need the code to be portable.
What can I do?
Thanks.

You can configure asset manager's baseUrl in /backend/config/main.php
'components' => array(
...
'assetManager' => array(
'class' => 'CAssetManager',
'baseUrl' => 'frontend.assets'
),
...

Related

Yii Alias or Params

Which one is better to set directory folder for global usage?
Below are set to yii2 web config.
'aliases' => [
'#uploads' => 'uploads/csv',
],
Or in
'params' => [
'adminEmail' => 'admin#example.com',
'uploadCsv' => 'uploads/csv/',
];
If I am using the Yii Advanced template I prefer using the alias option.For the Yii basic template any will be fine.
Example: in the common/config/bootstrap.php of Advanced template
Yii::setAlias('#uploads', dirname(__DIR__) . '/uploads/csv');

Yii2 load modules

how I can load an external-site module? I have a common module I need to load in distinct Yii2 sites, like advanced-template my idea is to have a common dir where store generic modules wich I can load to each site. A file system structure can be like this:
/
site-1/
(loads modules from common-modules dir for site-1)
site-2/
(loads modules from common-modules dir for site-2)
common_sites_modules/
module-1/
module-2/
carrello/
Carrello.php
Each site in his configuration have to load modules from common-modules/
Is possible to implement this structure?
Edit 1
The configuration:
'cart' => [
'class' => dirname(dirname(dirname(__DIR__))) . '/common_sites_modules/carrello/Carrello',
'params' =>[
...
],
'components' => [
...
],
],
and this is the first line of the class Carrello.php:
<?php
namespace common_sites_modules\carrello;
...
The top bar of editor with the path of the class and the error returned by Yii:
Edit 2:
Thanks to #Yupik for support and suggests, the new settings:
bootstrap.php:
Yii::setAlias('#common-modules', dirname(dirname(dirname(__DIR__))) . '/common_sites_modules');
main-local.php:
'class' => '#common-modules\carrello\Carrello',
The generated error:
Like suggested in the comments the solution is to declare an alias and then use the name of alias for call the module. Like suggested by #Yupik I've set in the common/config/bootstrap.php an alias as follow:
Yii::setAlias('#common_modules', dirname(dirname(dirname(__DIR__))) . '/common_modules');
In the main configuration:
'carrello' => [
'class' => ''common_modules\carrello\Carrello',
...
]
Obviously namespace have to be configured based on the position on filesystem.
Thanks for the suggestions
Yes, we can do this by making a symlink of common_sites_modules directory in both site folder (site1, site2).

Gii doesn't load in custom build of Yii2

I'm using the Foundationize custom build of Yii2, which is a couple of years old but in general seems to run just fine.
However, when I've come to run the gii tool for generating new models etc. the routing is ignored and my application's index page is simply reloaded.
The URL I'm trying to access is http://localhost/web/index.php?r=gii
I have this line in my index.php
defined('YII_ENV') or define('YII_ENV', 'dev');
and my config/web.php contains the following
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => [$_SERVER['REMOTE_ADDR']], // always allow on current server
];}
Am i missing a step somewhere? I tried the 'basic' setup from the main Yii site and that seemed to work fine on the same server. Thanks in advance.
For some reason it was the routing that was broken in this install
instead of
http://localhost/web/index.php?r=gii
I used
http://locahost/web/index.php/gii
and it was all fine
I think you modified your configure ( in config/web.php)
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager', //clase UrlManager
'showScriptName' => true, // to remove index.php
'enablePrettyUrl' => true //for Friendly url
],
],
If you use the advanced template, try
http://localhost/mysitename/frontend/web/index.php/gii
Or
http://localhost/mysitename/frontend/web/index.php?r=gii

How to change URL to asset->sourcePath on Yii2?

I have folder (e.x.: /modules/admin/static/) out of web/ and I specified sourcePath: /modules/admin/static/.
Yii generating assets files in web/assets/13dfh1/.
But I want to change this folder to web/static/assets/13dfh1/.
I want have url: http://example.com/static/assets/13dfh1/.
baseUrl and basePath ignored if specified sourcePath.
It's must for another depends, such as YiIAsset, debug, and etc.
Please, help me.
P.S. Sorry for my egnlish )
If you want change the asset folder you can change the basePath in assetManager in your \config\main.php
'components' => [
'assetManager' => [
'basePath' => '#webroot/your-assets-dir',
],
.......
Else you can do it by the code
\Yii:$app->assetManager->basePath = '#webroot/your-assets-dir';

Yii framework: Controller url & parameters

In my application I have "PController" with default action "actionIndex", so in Yii the path becomes p OR p/index. Now in order to get certain users info I use the following path p/index?u=test where test is the username and u part of the path is basically a GET parameter (p/index?u=test).
Is there any way to do the same thing without index?u= part of the path, i.e. I want my path to look like example.com/p/test ?
In your application config find section for components and add options for urlManager
array(
......
'components' => array(
......
'urlManager'=>array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'/p/<u:\w+>'=>'p/index'
),
),
),
);
http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Categories