2016-08-19 137 views
3

我的问题涉及将CometChat与laravel 5.1集成。CometChat与laravel的集成5.1

我试图在Cometchat的integration.php文件中访问Laravel。我想访问Session类,这样我就可以从数据库访问会话信息(默认Cometchat使用文件会话)。 目前我已经转换Laravel使用文件会话。

那么我怎样才能访问Laravel的会话,以便我可以在integration.php文件中访问它?

回答

1

好的我认为我已经完成了。以下代码使我可以访问现有的Laravel应用程序,并且可以访问Session甚至Sentinel。

我还添加了一个指向vendor/autoload.php的include,它现在可以访问QueryBuilder和其他系统。

在integration.php顶我:

// integration.php includes the laravel files to give access, it just 
// didn't use it fully 

$app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture()); 

$id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]); 
$app['session']->driver()->setId($id); 
$app['session']->driver()->start(); 

这将返回当前运行Laravel,我就能够做这样的事情$app['session']->get('dataname')

虽然与添加的供应商/自动加载。 PHP我现在也可以访问DB::tableSentinel::getUser()

0

请在include_once(dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'app.php');行后添加以下行。

include_once(dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR.'Auth'.DIRECTORY_SEPARATOR.'app.php'); 

与integration.php文件替换为以下getUserID()函数,或者你可以从http://my.cometchat.com

function getUserID() { 
    $userid = 0; 
    if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') { 
     $_REQUEST['basedata'] = $_SESSION['basedata']; 
    } 

    if (!empty($_REQUEST['basedata'])) { 
     if (function_exists('mcrypt_encrypt') && defined('ENCRYPT_USERID') && ENCRYPT_USERID == '1') { 
      $key = ""; 
      if(defined('KEY_A') && defined('KEY_B') && defined('KEY_C')){ 
       $key = KEY_A.KEY_B.KEY_C; 
      } 
      $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode(rawurldecode($_REQUEST['basedata'])), MCRYPT_MODE_CBC, md5(md5($key))), "\0"); 
      if (intval($uid) > 0) { 
       $userid = $uid; 
      } 
     } else { 
      $userid = $_REQUEST['basedata']; 
     } 
    } 
    if (!empty($_COOKIE['laravel_session'])) {  
     $app = app(); 
     $kernel = $app->make('Illuminate\Contracts\Http\Kernel'); 
     $response = $kernel->handle(
      $request = Illuminate\Http\Request::capture() 
      ); 
     $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]); 
     $app['session']->driver()->setId($id); 
     $app['session']->driver()->start(); 
     if($app['auth']->user()!= NULL){ 
      $userid = $app['auth']->user()->id; 
     } 
    } 
    $userid = intval($userid); 
    return $userid; 
} 
+0

下载CometChat包Laravel 5虽然我能做到这一点,添加额外现在包括使我可以直接访问会话,甚至更好的Sentinel,这是当前正在使用的用户认证软件包。因此,而不是大功能,它只是成为一行:'''返回Sentinel :: getUser() - > ID;''' – zetetic