2017-03-26 1 views
0

私はSymfony2のラチェットとmemcacheのを使用して、私はmemcacheのは、私がphp.iniファイルにこの行を編集したSymfony2の:名前空間のクラス「のMemcache」をロードしようとしました

sudo apt-get install php-memcache -y 

、このコマンドラインを使用してインストール

session.save_handler=memcache 
session.save_path="localhost:11211" 

が、私は

$memcache = new Memcache; 

このラインを使用するとき、私はこのエラーを取得する

symfony2: Attempted to load class "Memcache" from namespace 

私のコードsocketcommand.php

<?php 
namespace check\roomsBundle\Command; 
use Ratchet\Session\SessionProvider; 
use Symfony\Component\HttpFoundation\Session\Storage\Handler; 
use Ratchet\App; 
use Symfony\Component\Console\Command\Command; 
use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 

// Include ratchet libs 
use Ratchet\Server\IoServer; 
use Ratchet\Http\HttpServer; 
use Ratchet\WebSocket\WsServer; 
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 
// Change the namespace according to your bundle 
use check\roomsBundle\Sockets\Chat; 
use Doctrine\ORM\EntityManager; 
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler; 
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; 

class SocketCommand extends ContainerAwareCommand 
{ 
    protected function configure() 
    { 
     $this->setName('sockets:start-chat') 
      // the short description shown while running "php bin/console list" 
      ->setHelp("Starts the chat socket demo") 
      // the full command description shown when running the command with 
      ->setDescription('Starts the chat socket demo') 
     ; 
    } 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $output->writeln([ 
      'Chat socket',// A line 
      '============',// Another line 
      'Starting chat, open your browser.',// Empty line 
     ]); 
     $memcache = new Memcache; 
     $memcache->connect('localhost', 11211); 

     $session = new SessionProvider(
      new Chat(new MyApp(), $this->getContainer()), 
      new Handler\MemcacheSessionHandler($memcache) 
     ); 

     $server = new App('localhost'); 
     $server->route('/sessDemo', $session); 
     $server->run(); 
    } 

} 

私chat.php

<?php 
namespace check\roomsBundle\Sockets; 
use tuto\testBundle\Entity\Users; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Ratchet\WebSocket\WsServerInterface; 
use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Doctrine\ORM\EntityManager; 
use Symfony\Component\DependencyInjection\ContainerInterface as Container; 
use Symfony\Component\HttpFoundation\Session\Session; 


class Chat extends Controller implements MessageComponentInterface, WsServerInterface 
{ 
    protected $container; 
    protected $clients; 

    //protected $em; 

    //protected $db; 
    public function __construct(ContainerInterface $container) { 
     $this->clients = new \SplObjectStorage; 
     $this->container = $container; 


     //$this->em = $em; 
     //$this->container = $container; 
     //$this->em = $em; 
    } 

    public function onOpen(ConnectionInterface $conn) { 
     $this->clients->attach($conn); 

     if (!isset ($conn->Session) || !$conn->Session instanceof Session) { 
      throw new \RuntimeException('Session is not defined. Make sure that SessionProvider is executed before FOSUserProvider.'); 
     } 
     try { 
      $token  = unserialize($conn->Session->get('_security_main')); 
      $user  = $token->getUser(); 
      $provider = $this->_container->get('fos_user.user_provider.username'); 
      $conn->User = $provider->refreshUser($user); 
     } catch (Exception $ex) { 
      $conn->User = null; 
     } 
    } 
+0

試してみると、このライン$memcache = new Memcache;

を置き換える「$ memcacheの=新しいです\ Memcache; '末尾にバックスラッシュが付きます –

+0

@FrankB did't work – mrsharko

+0

@FrankB私はあなたのコードをagaianで試してみました。 – mrsharko

答えて

0

この$memcache = new \Memcache;

関連する問題