Generic Actions / Graphical Things In A Class For Views

Hi everybody,

Here is my question: I implement in every Admin view of my models some custom Chtml::Link and Chtml::Ajaxlink which are, almost same, except some variable as the grid’s name. I put that code in my generator in order to have what I want in every view without coding anything. However when I want to change something in one of my button, for example, I have to do it X times, X corresponding of numbers of model I have. So I would like to know if it’s possible to create a class, somewhere, where I’d put all of this item with some parameters. Thanks to that, I would change only my class method chtml::link instead of do it for every admin view.

As it more graphical things, such as “echo chtml::link(…)”, I don’t really know where I can create my class, and how I can call it in my admin.php…

If you have any ideas =)

thanks

If you want to create a reusable action, use CAction.

If you want to generate the link in a more standard way, you can create a widget in which you create the part of view that you reuse.

Zaccaria’s advice is very good. You can follow it and create a widget.

You can add your widget class to the components directory of your app.

For instructions on creating a widget, please see the guide.

You will find instructions on using your new widget on the guide also, here and here.

I suggest that you read the whole guide, though. It is a great resource to learn the framework.

thanks for answering and readings, I created finally my widgets but it doesn’t work and I don’t find my mistake can you just check what can be wrong :

widget :


<?php

class ActionButton extends CWidget {


    public $title='My title';

    public $directToLink='';

    public $csrf='';

    public $id='';

    public $src='/myApp/images/myImage.gif';

    public $style='height:10px; width:10px; padding:0px 2px 2px 0px;';

 

    public function run() {

        $this->render('actionButton');

    }

 

}

?>

widget’s view :


<div id="actionButton">   

<?php

  Yii::app()->getClientScript()->registerScriptFile(Yii::app()->baseUrl."/js/functions.js");


    echo CHtml::link('<button type="button"><img id="'.$this->id.'" src="'.$this->src.'" style="'.$this->style.'"/>'.$this->title.'</button>',

                   "",

                   array('onclick'=>"{addElement('".$this->directToLink."','#dialogBox','".$this->csrf."'); $('#dialogBox').dialog('open');}",'id'=>'addElement')

                  );

?> 


  <?php  $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog

    'id'=>'dialogBox',

    'options'=>array(

        'title'=>'Create Element',

        'autoOpen'=>false,

        'modal'=>true,

        'width'=>350,

        'height'=>340,

    ),

));?>


<div class="divForForm"></div>

 

<?php $this->endWidget();?>

</div>

in my view :


<?php  $csrf=Yii::app()->request->csrfTokenName.'='.Yii::app()->request->csrfToken;

        $this->widget('application.components.widgets.ActionButton', array(

        'directToLink'=>$this->createUrl(''),

        'csrf'=>$csrf,

        'src'=>'/myNewApp/images/add.gif',

        'id'=>'Add2Img',

        ));

  

  ?>

page loads until to reach the widget declaration… firebug sends me an internal server error

The "echo" I put in my widget, works if I write it directly in the view.

What is the error message you’re receiving?

If you see no details of the error, you can enable the debug mode on your app’s index.php file.

I got a “500 server internal error”… I activated debug but, is it working in case of internal error ?? often it’s pointing a syntax error somewhere in the code… but here I cannot see where could be the error.


// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each log message

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',1);



Where can see log debug message ? is trace level correct ? I never got this working to be honest, is it logging in a file or directly on screen ? though it seemed to me that my index.php was ok to get debug log.

You can check your application log (by default, the file application.log in the runtime directory), the PHP log file and the Web server logs to find the information about it.

If you just throw an exception in your controller, can you see the Yii’s default error page?




throw new CHttpException(500, 'Testing.');



Sorry for delay… I had to leave…

Well, yes I can see the "error 500 testing" on page if I put the exception in my controller… However nothing in application.log of yii

Please notice that by “Yii’s default error page” I mean a page with a call stack and more information besides the error code and message.

To have an application log file you should have logging enabled on your config file. Please also check this.

there was no call stack… before adding “throw new CHttpException(500, ‘Testing.’);” and also after

Correct me if I’m wrong, but don’t you need to pass the properties to the render method.




public function run() {        

    $this->render('actionButton');    

}


// Should be 


public function run() {        

    $this->render('actionButton', array(

         'directToLink' => $this->directToLink,

        ...

    ));    

}



Matt

Hi Matt,

In this case, [font="Courier New"]directToLink[/font] is a property of the controller class. The view has access to the controller instance via [font="Courier New"]$this[/font], so it is not needed.

Zugluk,

I’m sorry, but I have never had this issue. I don’t have a specific suspect, but my suggestions for you to take a look at are:

  • How CApplication::errorHandler is configured.

  • How CErrorHandler is configured.

  • Whether the route and view referenced above exist.

I hope somebody can shed some light on this issue.

Thanks very much for your help

Don’t you think, rather than to create a widget, I should not create a simple php file with my echo function and include it in my views file ?

You can do it, but I don’t recommend that you do it.

Why not?

Maybe that’s just me, but I feel more organized if I follow the Yii way of doing it.