我安装了全新的yii2-app-advanced,想加入Yii::$service功能,报严重错误,请教下楼主

问题咨询 · tgy3300 · 于 4年前 发布 · 1934 次阅读

我的操作步骤如下: 1、composer create-project yiisoft/yii2-app-advanced myapp 2、cd fecshop 3、./init 4、./yii migrate

安装yii2成功

5、我在 frontend\web\index.php 文件加入如下代码:

$config = yii\helpers\ArrayHelper::merge(
    ...
	require __DIR__ . '/../../common/config/services.php'
	...
)

new common\services\Application($config['services']);
unset($config['services']);

6、复制 fancyecommerce\fecshop\services\Application.php 文件和 fancyecommerce\fecshop\services\Service.php 文件到 common\services\ 下,并把 namespace 的命名空间改到common下

7、复制 fancyecommerce\fecshop\config\fecshop.php 文件到 common\ 下,并改名为 services.php,并新建common\config\components 和 common\config\services 两个目录

8、复制 fancyecommerce\fecshop\config\services\Cart.php 文件到 common\config\services\ 下

9、然后在 frontend\web\index.php 文件中打印 print_r($config['services']); 结果如下

Array ( [cart] => Array ( [class] => fecshop\services\Cart [childService] => Array ( [quote] => Array ( [class] => fecshop\services\cart\Quote ) [quoteItem] => Array ( [class] => fecshop\services\cart\QuoteItem ) [info] => Array ( [class] => fecshop\services\cart\Info [maxCountAddToCart] => 100 [addToCartCheckSkuQty] => ) [coupon] => Array ( [class] => fecshop\services\cart\Coupon ) ) ) )

10、但 Yii::$service 功能还是不能使用,报如下错误,大佬指点下,还差什么

Fatal error: Uncaught Error: Access to undeclared static property: Yii::$service in /wwwroot/myapp/common/services/Application.php:68 Stack trace: #0 /wwwroot/myapp/frontend/web/index.php(25): common\services\Application->__construct(Array) #1 {main} thrown in /wwwroot/myapp/common/services/Application.php on line 68
共收到 3 条回复
Fecmall#14年前 0 个赞

fecshop的Yii.php进行了重写:

https://github.com/fecshop/yii2_fecshop_app_advanced/blob/master/environments/dev/appfront/web/index.php#L20

你需要添加这个Yii.php文件,并在index.php中调用这个Yii.php文件

tgy3300#24年前 0 个赞

优秀,我试试

successgo#34年前 1 个赞

其实还有另外一种方式来实现。 将 service 当作一个 component 实现,用法就如:

Yii::$app->service->order->exampleMethod();
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics