2017-07-18 49 views
1

我通过扩展引导方法来启动我的Laravel应用程序,其中我有一个检查缓存中的值的小检查点。对于这个我使用Laravel's自己Illuminate\Foundationhelper functioncache,但不幸的是我得到和错误,我的应用程序代码是:如何在Laravel中使用Cache辅助函数

<?php 

namespace App; 

use Illuminate\Foundation\Application as IlluminateApplication; 
use LayerShifter\TLDExtract\Extract; 

/** 
* Extends \Illuminate\Foundation\Application to override some defaults. 
*/ 
class Application extends IlluminateApplication 
{ 
    /** 
    * Is Client. 
    * 
    * @var boolean 
    */ 
    protected $isClient = false; 

    /** 
    * Client secret key. 
    * 
    * @var boolean 
    */ 
    protected $clientSecret; 

    /** 
    * Client ID. 
    * 
    * @var boolean 
    */ 
    protected $clientID; 

    /** 
    * Constructing the class with tenant check 
    */ 
    public function __construct($basePath = null) 
    { 
     parent::__construct($basePath); 

     $this->clientCheck(); 
    } 

    public function clientCheck() 
    { 
     if($this->isClient = cache('is_client')) 
     { 
      return $this->isClient; 
     } 
     else 
     { 
      $domainData = $this->getDomainSubDomain(); 
      //Do Check and return the value, 
      //Set the values 
      // Cache for one day 
      $data = //Data which is being recieved 
      cache(['is_client' => $data], 1 * 24 * 60); 
      return $data; 
     } 
    } 

    /** 
    * Get Domain and Sub Domain 
    * 
    * @return array 
    */ 
    public function getDomainSubDomain() 
    { 
     $http_req = php_sapi_name() == 'cli' ? 'noetic.com' : $_SERVER["SERVER_NAME"]; 
     $extract = new Extract(); 
     $result = $extract->parse($http_req); 
     return array(
      "domain" => $result->getHostname() . '.' . $result->getSuffix(), 
      "subDomain" => $result->getSubdomain() 
     ); 
    } 
} 

错误,我越来越:

Fatal error: Uncaught ReflectionException: Class cache does not exist in 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php:729 Stack trace: #0 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(729): ReflectionClass->__construct('cache') #1 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(608): Illuminate\Container\Container->build('cache') #2 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(575): Illuminate\Container\Container->resolve('cache') #3 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(728): Illuminate\Container\Container->make('cache') #4 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(106): Illuminate\Foundation\Application->make('cache') #5 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(230): app('cache') #6 
E:\ in E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 729 

指引我我怎么能做到这一点。

+0

你试过用'app(“cache”)'? –

+0

也许缓存服务尚未包含在内。尝试在控制器中使用该功能,如果这可以正常工作,那么显然问题是应用程序尚未启动缓存方法。 –

+0

@ParantapParashar:缓存正在通过控制器工作,但我的问题是我想在加载应用程序之前进行缓存存储检查,我有不同的环境需要加载相应的不同域,FYI你可以看看在这个应用程序https://github.com/phanan/koel/blob/master/app/Application.php#L88 –

回答

-1

我通常使用Redis的这个让我按照以下步骤

composer require predis/predis 

然后去.ENV文件,并改变我的CACHE_DRIVER=fileCACHE_DRIVER=redis

,然后设置数据我用

Cache::put('key', 'value', $expiresAt); 

并获取我使用的数据

Cache::get('key'); 
+0

这对他的问题没有帮助。这是一个_alternative_,但它不是解决他们的问题。 – James