Yii框架没有从库中获取函数到控制器(使用class.php文件)


Yii framework not getting functions from library into controller (using class.php files)

我正在使用一个名为MetaTune的Spotify库,并且能够在CodeIgniter中轻松做到这一点,但在Yii中存在一些初期问题,但目前它已经开始说:

Fatal error: Call to undefined method stdClass::searchTrack() in ....public_html/Yii/news/protected/controllers/NewsController.php on line 67

然而,函数是存在的。这个库中的文件都有一个.class.php后缀(例如MetaTune.class.php),并且库文件都存储在:

yii/application/protected/vendors/Metatune

与Codeigniter我做了一个额外的spotify.php文件夹外,并自动加载到我的控制器,但我不确定这是必要的。

我已经将它加载到我的config.php中:

    'import'=>array(
    'application.models.*',
    'application.components.*',
            'application.vendors.metatune.*',
),
下面是Controller代码:
public function actionView($id)
{
 $model=$this->loadModel($id);
$spotify = MetaTune::getSomething();
$hello =  $model->title;
Yii::import('application.vendors.metatune.MetaTune');
$spotify->autoAddTracksToPlayButton = true; // Will add all searches for tracks into a list.
$spotify->playButtonHeight = 330; // For viewing the entire playlist
$spotify->playButtonTheme = "dark"; // Changing theme
$spotify->playButtonView = "coverart"; // Changing view
try
{
    $tracks = $spotify->searchTrack($hello);
    $tracks = $spotify->getPlayButtonAutoGenerated($hello);
}
catch (MetaTuneException $ex)
{
    die("<pre>Error'n" . $ex . "</pre>");
}
$song = 'tracks';
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}

还请参阅下面的代码,它有一个名为getInstance的函数,由于某种原因与Yii不能很好地工作,我不确定我是否可以改变这一点,因为我用它来导入MetaTune到CodeIgniter控制器没有任何问题。

只是MetaTune.class.php代码的一部分:

 Yii::import('application.vendors.metatune.Artist');
 Yii::import('application.vendors.metatune.Album');
 Yii::import('application.vendors.metatune.Track');
 Yii::import('application.vendors.metatune.CacheRequest');
 Yii::import('application.vendors.metatune.MBSimpleXMLElement');
 Yii::import('application.vendors.metatune.SpotifyItem');
 Yii::import('application.vendors.metatune.MetaTuneException');
 ....
 class MetaTune {
const CACHE_DIR = 'application/vendors/metatune/cache/'; // Cache directory (must be writable) relative to this file
const USE_CACHE = false; // Should caching be activated?
const CACHE_PREFIX = "METATUNE_CACHE_"; // prefix for cache-files. 
const SERVICE_BASE_URL_SEARCH = "http://ws.spotify.com/search/1/";
const SERVICE_BASE_URL_LOOKUP = "http://ws.spotify.com/lookup/1/";
const PLAYBUTTON_BASE_URL = "https://embed.spotify.com/?uri=";
public $autoAddTracksToPlayButton = false;
private $list = array();
// Holds instance
private static $instance;
.....
public static function getSomething()
{
    if (!isset(self::$instance))
    {
        $class = __CLASS__;
        self::$instance = new $class;
    }
    return self::$instance;
}
.....
 public function searchTrack($name, $page = 1)
{
    $url = self::SERVICE_BASE_URL_SEARCH . "track?q=" . $this->translateString($name) .  
    $this->addPageSuffix($page);
    $contents = $this->requestContent($url);
    $xml = new MBSimpleXMLElement($contents);
    $tracks = array();
    foreach ($xml->track as $track)
    {
        $tracks[] = $this->extractTrackInfo($track);
    }
    if ($this->autoAddTracksToPlayButton) {
        $this->appendTracksToTrackList($tracks);
    }
    return $tracks;
    } 
如果你有任何建议,我将非常感激。谢谢。

您没有在任何地方初始化$spotify, php默认情况下将其变为stdClass,因为您正在使用该变量为成员属性分配值,但是当您尝试调用不存在的方法时失败了。

解决方案:在使用它之前初始化它

$spotify = MetaTune::getInstance();