lonnieezell / Bonfire

Jumpstart your CodeIgniter web applications with a modular, HMVC-ready, backend.
http://cibonfire.com
1.39k stars 524 forks source link

modules::run returning a null result #784

Closed VeZoul closed 11 years ago

VeZoul commented 11 years ago

I have a controller that lists some offers I created. Foreach offer, I want to display the number of unread messages associated.

I thought using modules::run but when I call :

<?php echo Modules::run('messages/messages/get_number_new_messages/'.$oOffer->offer_id); ?>

Nothing is displayed. If I call the get_number_new_messages directly in my url, it returns me the int I want (ie: "1")

Here is my messages controller function :

public function get_number_new_messages($bForOffers = false){
        $iNumberMessages = $this->messages_model->count_new_messages($this->current_user->previous_last_login, $bForOffers);

        $aDatas = array();
        $aDatas['iNumberMessages'] = $iNumberMessages;

        return $this->load->view('messages/get_number_new_messages', $aDatas);
    }

I also call

<?php echo Modules::run('messages/messages/get_number_new_messages/a'); ?>

and

<?php echo Modules::run('messages/messages/get_number_new_messages'); ?>

in one of my theme file

Am I missing something... ?

artlantis commented 11 years ago

How you access $aDatas in view?

VeZoul commented 11 years ago

This is the content of the view :

<?php echo $iNumberMessages; ?>
artlantis commented 11 years ago

it should be

echo set_value('iNumberMessages', isset($aDatas) ? $aDatas['iNumberMessages'] : '' 
sourcejedi commented 11 years ago

No, set_value helps with forms / preserving user input on validation failure. I don't think there's any reason to use it here.

artlantis commented 11 years ago

then, just use

$aDatas['iNumberMessages']
sourcejedi commented 11 years ago

Nope, that's definitely not it either.

VeZoul commented 11 years ago

Does not work either indeed

sourcejedi commented 11 years ago

I'm not sure what the problem is yet. However I think it would be better design to put the method in a model (or possibly a library), and have it return a PHP integer.

You can then load the model/library from any controller.

artlantis commented 11 years ago

interesting, I can use this in my view as well, it's working properly! I have same type data array which I can access array value by name.

sourcejedi commented 11 years ago

Also HMVC (i.e. Modules) is kinda un-loved. I don't think it's caused us much trouble in Bonfire, but if there's no specific reason to tie yourself to it, then I'd avoid Modules::run() :).

artlantis commented 11 years ago

Sample :

#controller
                $this->load->model('customer/customer_model');
                $this->db->select('core_customers.*, storename')->from('core_customers')->join('core_stores', 'core_customers.homestore = core_stores.storeid')->where('userid', $this->current_user->id);
                $query = $this->db->get();

                $customer = array(
                    'metrono'       => $query->row()->metrono,
                    'shortname'     => $query->row()->shortname,
                    'homestore'     => $query->row()->storename,
                    'taxoffice'     => $query->row()->taxoffice
                );

                Template::set('customer', $customer);
#view
<input class="span4 uneditable-input" type="text" id="homestore" name="homestore" value="<?php echo set_value('homestore', isset($customer) ? $customer['homestore'] : '') ?>"/>
VeZoul commented 11 years ago

Ok, it seems that the problem is caused by the parameter passed to the function get_number_new_messages This means that :

<?php echo Modules::run('messages/messages/get_number_new_messages'); ?>

works where that :

<?php echo Modules::run('messages/messages/get_number_new_messages/15'); ?>

doesn't

I'll see if I really need to use modules::run as you said and if it's the case, I'll create differents functions

sourcejedi commented 11 years ago

@artlantis Your code is different. Please compare your posted code with @VeZoul's posted code.

I know debugging is a difficult skill. Perhaps you could make it clearer when your comments are suggestions, and you're not 100% sure about what's going on. Thanks.

artlantis commented 11 years ago

I don't related with your problem but in HMVC site, there is quote like below :

Notes:

_To use HMVC functionality, such as Modules::run(), controllers must extend the MXController class.

To use Modular Separation only, without HMVC, controllers will extend the CodeIgniter Controller class.

You must use PHP5 style constructors in your controllers*

sourcejedi commented 11 years ago

@VeZoul haha, I can imagine that.

If you look at the docs at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc, the examples show params being passed separately. I.e. Modules::run('messages/messages/get_number_new_messages', 15) - maybe that would work.

VeZoul commented 11 years ago

Ahhh you saved my day ! I missed that line it seems... Indeed it looks like it works ! Thanks !!! :+1:

sourcejedi commented 11 years ago

@artlantis Good thought, but it should be covered in most cases. Bonfire's Controller classes already extend MX_Controller. So as long as you extend our Front_Controller / Admin_Controller etc., it should cover that requirement.

artlantis commented 11 years ago

@sourcejedi @VeZoul good cooperation, I am glad to solve your problem. Happy coding :)