voryx / ThruwayBundle

Bundle for building Real-time Apps in Symfony
98 stars 47 forks source link

How to use Register with location setting? #15

Closed cipherchien closed 9 years ago

cipherchien commented 9 years ago

Hi,

I try to use a register controller, but I got some problems. Here is my config setting and code:

voryx_thruway:
...
    locations:
#        bundles: ["OZLineBundle"]
        files:
            - "OZ\\LineBundle\\Controller\\RegisterController"

and the controller:

namespace OZ\LineBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Voryx\ThruwayBundle\Annotation\Register;

class RegisterController extends Controller
{
    /**
     * @Register("user.line.open")
     */
    public function userLineOpenAction($threadID)
    {
        try {
            $user = $this->container->getUser();  //this will make the action doesn't work.
            return $user->getUsername();   // it will make client get nothing
//            return $threadID;
        } catch (\Exception $exc) {
            return $exc->getMessage();
        }   //eof try          
    }     

the client side code:

Connection.session.call('user.line.open', [ Defaults.threadID ]).then(function(arg){
    console.log(arg);
});    

If the action is like this, the register can work and the client can response the response data.

    /**
     * @Register("user.line.open")
     */
    public function userLineOpenAction($threadID)
    {
           return $threadID;        
    }     

What's error with my code? And how to get container service in Register action?

Thank you.

davidwdan commented 9 years ago

What type of authentication are you using?

cipherchien commented 9 years ago

I found the solution.

class AnnotationConfigurationPass implements CompilerPassInterface
{
    ......
    public function process(ContainerBuilder $container)
    {
        ......
        foreach ($files as $class) {
            .......
            if ($class->implementsInterface('Symfony\Component\DependencyInjection\ContainerAwareInterface')) {
                $container->setDefinition($serviceId, $definition)
                    ->addMethodCall('setContainer', [new Reference('service_container')]);
            } else {
                $container->setDefinition($serviceId, $definition);
            }
      ......

then I rewrite my Register code:

use Voryx\ThruwayBundle\Annotation\Register;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Register implements ContainerAwareInterface
{
    protected $container;

    public function setContainer(ContainerInterface $container = null) {
        $this->container = $container;
//        $user = $this->container->get('security.token_storage')->getToken()->getUser();    // It's not work and stuck here
        $managerUser = $this->container->get('fos_user.user_manager');
        $user = $managerUser->findUserBy(array('username'=>'ABC'));  // it's work        

        $logger = $this->container->get('oz_base.logger');
        $logger->info($user->getUsername());    
    }

    /**
     * @Register("user.line.open")
     */
    public function userLineOpenAction($userID, $threadID)
    {
        $logger = $this->container->get('oz_base.logger');
        $logger->info($userID);     
        $logger->info($threadID);     

        return "done";
cipherchien commented 9 years ago

I haven't implement authentication yet. If I use authentication, the solution will make different?