Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem about auto loading vendor in module.
  • I have almost same code in /fuel/app/classes/controller and /modules/test/classes/controller

    This is code in main app controller

    use \ElephantIO\Exception\ServerConnectionFailureException;

    class Controller_Index extends \Controller_BaseController
    {
        public function action_index()
        {
            $client = new \ElephantIO\Client(new \ElephantIO\Engine\SocketIO\Version1X('http://localhost:1337'));
            try {
                $client->initialize();
                $emit_result = $client->emit('broadcast', array('sayhi' => 'Hello php :: ' .date('Y-m-d H:i:s')));
                $client->close();
            } catch (ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }


    and This is code in module controller

    use \ElephantIO\Exception\ServerConnectionFailureException;

    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            $client = new \ElephantIO\Client(new \ElephantIO\Engine\SocketIO\Version1X('http://localhost:1337'));
            try {
                $client->initialize();
                $emit_result = $client->emit('broadcast', array('sayhi' => 'Hello php :: ' .date('Y-m-d H:i:s')));
                $client->close();
            } catch (ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }


    The code in main app controller works fine. It can catch the error and echo could not connect.
    But, the code in module controller always throw an error from Elephant.io source code. I could not use try, catch to prevent this.

    However if i started socket.io both code works fine.

    What happens with auto loader in the module?
  • Have you registered "\ElephantIO\Exception\ServerConnectionFailureException" class in bootstrap or config?
  • You say the error happens in the Elephant.IO code. It could very well be it expects to run in the global namespace, which app controllers do, but module controllers don't.

    Without any more details about the error, and the code that causes the error, it's difficult to comment.
  • This is details about the error.

    ElephantIO\Exception\ServerConnectionFailureException [ Error ]:
    An error occurred while trying to establish a connection to the server
    C:/my-sites/myapp/fuel/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php @ line 144



  • @kazumi
    Added class in bootstrap but not work.

    'ElephantIO\Exception\ServerConnectionFailureException' => dirname(APPPATH).'/vendor/wisembly/elephant.io/src/Exception/ServerConnectionFailureException.php',

    or

    'ServerConnectionFailureException' =>
    dirname(APPPATH).'/vendor/wisembly/elephant.io/src/Exception/ServerConnectionFailureException.php',

    not work.
  • I think we have problem with namespace.

    This is an easy example.
    http://php.net/manual/en/datetime.gettimestamp.php

    $date = new DateTime();
    I can run this code in any native.php or even fuel app controller.
    But not in FuelPHP module controller. (Class 'Test\DateTime' not found error)

    $date = new \DateTime();
    I must add \ to make it running in FuelPHP module controller.

    This is PHP itself class, it should no need \ (back slash) to works.


    Back to my case
    I can run it perfectly wit the code in topic inside main app controller but not in module controller. I think it is because namespace problem.
    I cannot directly change all vendor's source code to written in FuelPHP's module style. (I could but it's not good, i think)
    What can i do to change or fix this?
  • I think that you made a mistake when adding "use" statement in module controller, cause "\ElephantIO\Exception\ServerConnectionFailureException" is not in global namespace. 

    That said, you should have in your bootstrap.php something like this:
    'ServerConnectionFailureException' =>
    dirname(APPPATH).'/vendor/wisembly/elephant.io/src/Exception/ServerConnectionFailureException.php', 

    And in your module controller: 
    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (\ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }

     It must work.
  • @kazumi Same result, not working.

    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (\ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }

    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }


    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (ElephantIO\Exception\ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }


    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (\ElephantIO\Exception\ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
        }// action_index
    }

    None of these work.
  • HarroHarro
    Accepted Answer
    You can not use the "use" statement in combination with a try/catch, that needs the fully qualified classname.

    But I don't see why the last example wouldn't work. What happens if you do:


    namespace Test;

    class Controller_Admin_Elephantio extends \Controller_BaseController
    {
        public function action_index()
        {
            
            try {
                // code here...
            } catch (\ElephantIO\Exception\ServerConnectionFailureException $e) {
                echo 'could not connect.';
            }
            } catch (\Exception $e) {
                var_dump($e);
            }
     
      }// action_index
    }

  • The result is "could not connect." and $e is nothing.

    I can stop it from throwing error now. Thank you @kazumi and @Harro Verton

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion