Yii2 - Gii扩展资产文件夹别名,指向错误的路径。

时间:2022-10-05 08:55:46

I am trying to learn Yii 2 from a book (Web application development with Yii2 and PHP). Somewhere along the line it instructs me to install gii and create crud files with it.

我正在尝试从一本书中学习Yii2(使用Yii2和PHP开发Web应用程序)。在这一行的某个地方,它指示我安装gii并使用它创建crud文件。

When I installed with the following command:

当我安装了以下命令:

php composer.phar require --prefer-dist "yiisoft/yii2-gii:*"

I have following error:

我有以下错误:

Invalid Parameter – yii\base\InvalidParamException The file or directory to be published does not exist: /var/projectsRoot/crmapp/src/vendor/yiisoft/yii2/gii/assets

无效参数- yii\base\InvalidParamException将被发布的文件或目录不存在:/var/ projectsroot /crmapp/src/vendor/yiisoft/yii2/gii/资产。

My bootstrap code:

我的引导代码:

//Define Yii debug mode
define (YII_DEBUG, true);

//Including composer autoloader
require (__DIR__ . '/../vendor/autoload.php');

//Including Yii framework
require (__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

//debugging for PHP
ini_set('display_errors', true);

//Getting Configuration
$config = require(__DIR__ . '/../config/web.php');

//Include and launch application
(new yii\web\Application($config))->run();

config file:

配置文件:

return [
    'id' => 'crmapp',
    'basePath' => realpath(__DIR__ . '/../'),
    'components' => [
        'request' => [
            'cookieValidationKey' => 'your secret key here'
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false
        ],
        'db' => require(__DIR__ . '/db.php')
    ],
    'modules' => [
        'gii' => [
            'class' => 'yii\gii\Module',
            'allowedIPs' => ['192.168.33.1']
        ]
    ],
    'extensions' => [
        require (__DIR__ . '/../vendor/yiisoft/extensions.php')
    ]
];

extensions file:

扩展文件:

$vendorDir = dirname(__DIR__);

return array (
  'yiisoft/yii2-bootstrap' => 
  array (
    'name' => 'yiisoft/yii2-bootstrap',
    'version' => '2.0.5.0',
    'alias' => 
    array (
      '@yii/bootstrap' => $vendorDir . '/yiisoft/yii2-bootstrap',
    ),
  ),
  'yiisoft/yii2-gii' => 
  array (
    'name' => 'yiisoft/yii2-gii',
    'version' => '2.0.4.0',
    'alias' => 
    array (
      '@yii/gii' => $vendorDir . '/yiisoft/yii2-gii',
    ),
  ),
);

I digged it a little bit. It seems problem is about the alias of the assets folder.

我把它挖了一点。似乎问题在于资产文件夹的别名。

In GiiAsset.php file, there is this codeblock:

在GiiAsset。php文件,有这个代码块:

...
class GiiAsset extends AssetBundle
{
    public $sourcePath = '@yii/gii/assets';
...

which returns

它返回

/var/projectsRoot/crmapp/src/vendor/yiisoft/yii2/gii/assets

but it normally should return

但它通常会回来。

/var/projectsRoot/crmapp/src/vendor/yiisoft/gii/assets

so it is adding an unnecessary yii2 to the path.

所以它在路径上添加了不必要的yii2。

I tried to change the $sourcePath in extensions.php file, but changing the value here does not effect the result in any way.

我试着在扩展中改变$sourcePath。php文件,但是改变这里的值不会对结果产生任何影响。

Any ideas?

什么好主意吗?

--UPDATE--

While I was fiddling with things, I tried to define the alias to force the correct value; as follows:

当我在摆弄东西的时候,我试着定义一个别名来强制正确的值;如下:

Yii::setAlias('@yii/gii', $vendorDir . '/yiisoft/yii2-gii');

when I try to run the application with this setting I get following error:

当我尝试使用这个设置运行应用程序时,会出现以下错误:

The file or directory to be published does not exist: /var/projectsRoot/crmapp/src/vendor/bower/bootstrap/dist

要发布的文件或目录不存在:/var/ projectsroot /crmapp/src/vendor/bower/bootstrap/dist。

When I change the alias definition to this:

当我将别名定义改为:

Yii::setAlias('@yii/gii', $vendorDir . '/yiisoft/yii2-gi');

I get following error:

我得到以下错误:

The file or directory to be published does not exist: /var/projectsRoot/crmapp/src/vendor/yiisoft/yii2-gi

要发布的文件或目录不存在:/var/ projectsroot /crmapp/src/vendor/yiisoft/yii2-gi。

I'm quite confused with this behavior. What would be causing this?

我对这种行为很困惑。这会导致什么呢?

2 个解决方案

#1


0  

I ended up with deleting my vendor folder and composer.json file, and creating it back with following content:

我最后删除了我的供应商文件夹和作曲家。json文件,并以以下内容创建它:

{
    "require": {
        "codeception/codeception": "*",
        "fzaninotto/faker": "*",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-gii": "*"
    }
}

When I launched the gii, it again threw the following exception:

当我启动gii时,它又一次抛出了以下的异常:

The file or directory to be published does not exist: /var/projectsRoot/crmapp/src/vendor/bower/jquery/dist

要发布的文件或目录不存在:/var/ projectsroot /crmapp/src/vendor/bower/jquery/dist。

I renamed the vendor/bower-asset folder to vendor/bower and it works now.

我将供应商/bower-asset文件夹重新命名为供应商/bower,现在它可以工作了。

I probably messed up with something before without noticing, but I'm not certain why it is looking for bower, instead of bower-asset. Renaming the bower-asset to bower seems to solve it.

我可能在没有注意到之前就把事情搞砸了,但我不确定它为什么要找bower,而不是bower-asset。将bower-asset重新命名为bower似乎可以解决这个问题。

UPDATE

Thanks to jacmoe from the original Yii forum, it has finally solved.

多亏了最初的Yii论坛的jacmoe,它终于解决了。

It seems these two lines need do be present in composer.json in order to automatically create bower folder, instead of bower-asset.

这两条线似乎需要在作曲家中出现。json是为了自动创建bower文件夹,而不是bower-asset。

"extra": {
    "asset-installer-paths": {
        "npm-asset-library": "vendor/npm",
        "bower-asset-library": "vendor/bower"
    }
}

Original conversation can be found at here:

原始的对话可以在这里找到:

These lines are automatically created when you install the basic application template, but when installing the bare code base, you need to manually write them.

这些行是在安装基本应用程序模板时自动创建的,但是在安装裸代码库时,您需要手工编写它们。

#2


0  

I have similar problem with my app based on yii2-app-advanced, but separated from @common. Solution is just add vendorPath attribute to application config.

我的app基于yii2-app-advanced,但与@common分离,我也有类似的问题。解决方案只是将vendorPath属性添加到应用程序配置中。

#1


0  

I ended up with deleting my vendor folder and composer.json file, and creating it back with following content:

我最后删除了我的供应商文件夹和作曲家。json文件,并以以下内容创建它:

{
    "require": {
        "codeception/codeception": "*",
        "fzaninotto/faker": "*",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-gii": "*"
    }
}

When I launched the gii, it again threw the following exception:

当我启动gii时,它又一次抛出了以下的异常:

The file or directory to be published does not exist: /var/projectsRoot/crmapp/src/vendor/bower/jquery/dist

要发布的文件或目录不存在:/var/ projectsroot /crmapp/src/vendor/bower/jquery/dist。

I renamed the vendor/bower-asset folder to vendor/bower and it works now.

我将供应商/bower-asset文件夹重新命名为供应商/bower,现在它可以工作了。

I probably messed up with something before without noticing, but I'm not certain why it is looking for bower, instead of bower-asset. Renaming the bower-asset to bower seems to solve it.

我可能在没有注意到之前就把事情搞砸了,但我不确定它为什么要找bower,而不是bower-asset。将bower-asset重新命名为bower似乎可以解决这个问题。

UPDATE

Thanks to jacmoe from the original Yii forum, it has finally solved.

多亏了最初的Yii论坛的jacmoe,它终于解决了。

It seems these two lines need do be present in composer.json in order to automatically create bower folder, instead of bower-asset.

这两条线似乎需要在作曲家中出现。json是为了自动创建bower文件夹,而不是bower-asset。

"extra": {
    "asset-installer-paths": {
        "npm-asset-library": "vendor/npm",
        "bower-asset-library": "vendor/bower"
    }
}

Original conversation can be found at here:

原始的对话可以在这里找到:

These lines are automatically created when you install the basic application template, but when installing the bare code base, you need to manually write them.

这些行是在安装基本应用程序模板时自动创建的,但是在安装裸代码库时,您需要手工编写它们。

#2


0  

I have similar problem with my app based on yii2-app-advanced, but separated from @common. Solution is just add vendorPath attribute to application config.

我的app基于yii2-app-advanced,但与@common分离,我也有类似的问题。解决方案只是将vendorPath属性添加到应用程序配置中。