Yii2 - yii \ console \ Application :: getSession()

时间:2015-12-09 11:58:55

标签: php yii2 console-application yii2-advanced-app

我正在尝试从终端运行控制台应用程序,但我收到此错误

Calling unknown method: yii\console\Application::getSession()

这是控制器

class BeanstalkController extends \yii\console\Controller {

public function actionIndex() {

    echo 'this is console action';
} }

这是控制台配置

return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'modules' => [],
'components' => [
    'user' => [
        'class' => 'yii\web\User',
        'identityClass' => 'common\models\Users',
        'enableAutoLogin' => TRUE,
    ],
    'log' => [
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
],
'params' => $params,];

我尝试从配置中删除会话但仍然遇到会话错误。

任何人都可以提供帮助吗?

4 个答案:

答案 0 :(得分:0)

您无法使用 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> <script src="http://connect.facebook.net/en_US/all.js"></script> <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({appId: '1500007870291234', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); </script> <script type="text/javascript"> $(document).ready(function(){ $('#share_button').click(function(e){ e.preventDefault(); FB.ui( { method: 'feed', name: 'Ramalingam', link: 'http://temp.pickzy.com/ccc/', picture: 'http://temp.pickzy.com/ccc/images/1.PNG', caption: 'This is the content of the "caption" field.', description: 'This is the content of the "description" field, below the caption.' }); }); }); </script> <div class="share"> <a id="share_button" class='share-button' href=""><img src="image/fb_share.jpg" alt="" /></a> </div> ,因为Application::getSession()与网络应用不同。

http://www.yiiframework.com/doc-2.0/yii-console-application.html

尝试将会话组件添加到您的配置中。

yii\console\Aplication

并将其用作组件

return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'modules' => [],
    'components' => [
        'user' => [
            'class' => 'yii\web\User',
            'identityClass' => 'common\models\Users',
            'enableAutoLogin' => TRUE,
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'session' => [ 
            'class' => 'yii\web\Session'
        ],
    ],
    'params' => $params,
];

答案 1 :(得分:0)

所以我尝试的每个选项都不起作用的原因是我的Yii2版本已经老了,我不得不升级到最新版本以使一切工作完美。

感谢您的帮助。

答案 2 :(得分:0)

return [ 'components' => [ 'user' => null, // and put there other components which use a PHP-session ] ]

BuildWrapper

答案 3 :(得分:0)

控制台环境中的正确配置为:

'components' => [
            'user' => [
                'class' => 'yii\web\User',
                'identityClass' => 'app\models\User',
                'enableSession' => false,
                'enableAutoLogin' => false,
            ],
        ],