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.
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.
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
$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