How to render view of the module

I have been building the ImageUpload module. At first I created my module by the shell command. Then, I attached the ajax button in order to invoke a module action (it actually is a controller action in module).

<?php echo CHtml::ajaxButton('Upload', $this->createUrl('ImageUpload/default/index')); ?>


modules/ImageUpload/DefaultController.php:

<?php


class DefaultController extends CController


{


        public function actionIndex()


        {


          $this->render('index');


        }


}


modules/ImageUpload/views/default/index.php:

<p>


This is the view content for action "<?php echo $this->action->id; ?>".


The action belongs to the controller "<?php echo get_class($this); ?>" in the "<?php echo $this->module->id; ?>" module.


</p>


<p>


You may customize this page by editing <tt><?php echo __FILE__; ?></tt>


</p>


I have a module view file that is also generated by the shell, but I am not sure how to display it, since the display does not change at all when I click the button. Does anyone point the problem out?

Please check the generated HTML source about the ajax URL. You should use createUrl('index') or createUrl('default/index'), since the module ID will be automatically prefixed to it.

Thanks for the reply Qiang,

I had checked the generated HTML and nothing wrong was found.

jQuery('#yt0').click(function(){jQuery.ajax({'url':'/demos/yii-blogdemo-enhanced-modules/index.php?r=ImageUpload/default/index','cache':false});return false;});


I confirmed that the intentional syntax error was correctly produced when I executed following code.

protected/modules/ImageUpload/controllers/DefaultController.php:

<?php


class DefaultController extends CController


{


        public function actionIndex()


        {


          syntax error


          $this->render('index');


        }


}


On the other hand, following two cases are less working.

Case1:

<?php echo CHtml::ajaxButton('Upload', $this->createUrl('default/index')); ?>

HTML:

jQuery('#yt0').click(function(){jQuery.ajax({'url':'/demos/yii-blogdemo-enhanced-modules/index.php?r=default/index','cache':false});return false;});


Result:

Quote

[error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request "default/index".' in /var/www/html/yii/framework/web/CWebApplication.php:336

Case2:

<?php echo CHtml::ajaxButton('Upload', $this->createUrl('index')); ?>

HTML:

jQuery('#yt0').click(function(){jQuery.ajax({'url':'/demos/yii-blogdemo-enhanced-modules/index.php?r=post/index','cache':false});return false;});

Result:

Quote

[error] [exception.CHttpException.404] exception 'CHttpException' with message 'The system is unable to find the requested action "index".' in /var/www/html/yii/framework/web/CController.php:398

try:

<?php echo CHtml::ajaxButton('Upload', $this->createUrl('ImageUpload/default')); ?>

Yeah, this produced the same result as my first trial, because default module action is index.

<?php echo CHtml::ajaxButton('Upload', $this->createUrl('ImageUpload/default/index')); ?>

And it reached the line '$this->render('index');' as well, nothing is shown.

May be I am missing something in terms of ajax, e.g. header() or something, (though it is of no effect in this case), because the view has been rendered (finished). :cry:

mmm…

well here have a new tool to generate full working module crud…

maby adding your code for test… any ideia…

:(

http://www.yiiframew…sion/modulegen/

regards

I have read the article that says the ajax function sends some information from the client to the host asynchronously (It is OK so far). It says that the returned information from the host is received by ajax script(javascript)  ???

As there is no page display by using the following code, I think I should receive some information from the controller by using javascript, and should not use the controller to alter view.

Is this the right idea on using ajax?

<?php


class DefaultController extends CController


{


        public function actionIndex()


        {


          $this->render('index'); // surely here comes, but unfortunately, no page rendering here!


        }


}

Would you mind if you give me some words on this case, Qiang? Thank you in advance. :)

1.) your  module is working by default calling ?:

htpp://localhost/demos/yii-blogdemo-enhanced-modules/index.php?r=imageupload

2.)

I found this sample about calling ajax request:



if (Yii::app()->request->isAjaxRequest)


$this->renderPartial('listPage', compact('users', 'pages', 'sort'));


else


$this->render('list', compact('users', 'pages', 'sort'));


>megabr

  1. No. It produces the 404 error as below.

Quote

[error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request "imageupload".' in /var/www/html/yii/framework/web/CWebApplication.php:336
  1. I inserted Yii::trace method and found that it surely executes renderPartial because this is ajax request. But unfortunately, there is no new display on the screen. I should investigate header or something like that… :cry:

>Qiang

Is there such kind of method in Yii?

before do anything your module need work correct (route)

are you added the module imageupload correctly unde main.php file?

Quote

(I created a module called imageupload from module generator and update my config in main.php and working fine)

regards

As megabr suggested:

  1. check if you have configured the module in app config.

  2. Try directly accessing the action in browser (not via ajax) to make sure it works as expected.