Hello all,
Thanks for a great framework and lively conversation.
I'm weighing the merits of Module's maintainability/reusability over efficiency. it seems that the only way to use modules is via HMVC Requests, which inherently create another full stack webserver hit. This to me seems most inefficient. Here's my empty test case:
<code>
// fuel/app/modules/stats/classes/controller/buddy.php
<?php
namespace stats;
class Controller_Buddy extends \Fuel\Core\Controller {
public function action_donations () {
$this->response->body = "aawwww yeah";
}
}
</code>
Running this action through Request (HMVC) takes on average 300ms:
<code>
for ($x = 0; $x < 1000 ; $x++) {
Request::factory('stats/buddy/donations/id/' . $x, true)->execute()->response();
}
</code>
Whereas running through instantiation takes 15ms (~10ms of which is overhead, so really ~5ms to execute the loop)
<code>
require_once(APPPATH . 'modules/stats/classes/controller/buddy.php');
for ($x = 0; $x < 1000 ; $x++) {
$bud = new \stats\Controller_Buddy($this->request, $this->response);
$bud->action_donations();
}
// in this case the action method returns the value instead of setting the $response->body
</code>
I'm sold on the elegance of modules via hmvc code organization , but am having trouble balancing the costs in performance.
Is there any way to execute module actions taking advantage of hmvc routing without creating a full request? Alternatively, is there any way equally elegant to hmvc to call these actions? That would rock. If not, can you see any reason not to include this feature in Fuel? If not expect a pull request (or at least another derelict fork floating out there in the hub.)
I'm thinking of something like this but with better names (If I knew the names now i probably would realize that it's already been implemented):
<code>
\Fuel\Core\HMVCinator::grab($path);
</code>
Thanks in advance,
Ramble on,
David
The Request does a lot more than loading and calling the module controller method, so it's quite logical it's not that fast.
There are some optimizations possible that I'm currently looking at.
For example, you can set the third parameter to false if you don't want your request routed, which is usually the case if you call a module controller directly. This doesn't prevent the Request object from loading the routes, which could further reduce the loading time.
Also, I don't know which Fuel version you are using, but in 1.1/develop the route processing code has been optimized.
The downside of directly loading and calling the module controller is that the controller, and Fuel itself, is not aware it's running in a module context. Which means you're going to run into problems when loading config or lang files, loading module views, etc.