php 在 Yii 上创建小部件的步骤?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8447959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:44:05  来源:igfitidea点击:

Steps to create a widget on Yii?

phpyii

提问by MEM

I have the following code on a given view:

我在给定的视图上有以下代码:

<?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'home-newsletter-form',
        'enableAjaxValidation' => false,
        'enableClientValidation' => true,
            ));

    echo $form->textField($newsletterSubscribeForm, 'email');
    echo $form->error($newsletterSubscribeForm, 'email');
    echo CHtml::link("subscribe", "#", array('class'=>'btSubscribe'));
    $this->endWidget(); 
?>

It happens that I will need this on MORE then one view, so I find a widget a better option. I wish however to place this on a separate file (on app/widgets/ folder), and called on each view.

碰巧我会在更多然后一个视图上需要它,所以我发现一个小部件是更好的选择。然而,我希望将它放在一个单独的文件(在 app/widgets/ 文件夹上),并在每个视图上调用。

Can anyone please be kind enough to tell me what steps should we follow in order to achieve that?

任何人都可以告诉我我们应该采取哪些步骤来实现这一目标?

回答by emix

Widget is the best solution here, it will keep your code DRY (Don't Repeat Yourself - focus on re-usability) as well.

Widget 是这里最好的解决方案,它也会让您的代码保持干燥(不要重复自己 - 专注于可重用性)。

<?php

// protected/components/SubscriberFormWidget.php

class SubscriberFormWidget extends CWidget
{
    /**
     * @var CFormModel
     */
    public $form;

    public function run()
    {
        if (! $this->form instanceof CFormModel) {
            throw new RuntimeException('No valid form available.');
        }
        $this->render('subscriberFormWidget', array('form'=>$this->form));
    }
}

And the view:

和观点:

<?php
// protected/components/views/subscriberFormWidget.php

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'home-newsletter-form',
    'enableAjaxValidation' => false,
    'enableClientValidation' => true,
));

echo $form->textField($newsletterSubscribeForm, 'email');
echo $form->error($newsletterSubscribeForm, 'email');
echo CHtml::link("subscribe", "#", array('class'=>'btSubscribe'));
$this->endWidget();

sample usage inside any view

任何视图中的示例用法

<?php $this->widget('SubscriberFormWidget', array(
        'form' => $newsletterSubscribeForm
)); ?>

回答by Boaz Rymland

Creating a widget is very simple in Yii. It couldn't be better explained than in the following short official documentation section, Here.

在 Yii 中创建小部件非常简单。没有比以下简短的官方文档部分更好的解释了,这里

I little emphasis as lots of people found this answer useful. The following words are at my own taste, how I prefer designing my Yii application building blocks: when building your widget class always bear in mind that a widget is a kind of a view in Yii (v1.x). Its not supposed to process stuff, to perform important business logic decisions. Rather, as a view it merely supposed to renderstuff. The decision making code in it should be focused in finding out what to render. I used to design in the past widgets that included some AJAX processing. Today I think this is bad design. A widget should render stuff. Need an accompanying processing unit? I would pack it all in a module, with controllers, possibly model classes, and the widget as an extension in that module. Cest tout :-)

我很少强调,因为很多人发现这个答案很有用。下面的话是我自己的口味,我更喜欢设计我的 Yii 应用程序构建块的方式:在构建您的小部件类时,请始终牢记小部件是 Yii (v1.x) 中的一种视图。它不应该处理东西,执行重要的业务逻辑决策。相反,作为一个视图,它只是应该渲染东西。其中的决策代码应该专注于找出要呈现的内容。我过去常常设计包含一些 AJAX 处理的小部件。今天我认为这是糟糕的设计。小部件应该呈现东西。需要附带的处理单元吗?我会将它全部打包在一个模块中,带有控制器,可能是模型类,以及作为该模块中的扩展的小部件。Cest兜售:-)

回答by Asgaroth

You are better of using partials views.

你最好使用局部视图

Like this:

像这样:

<?php $this->renderPartial('//partials/_myview',
        compact('model', 'dataProvider')
); ?>

that way you can reuse the code in other views.

这样您就可以在其他视图中重用代码。

回答by Elango Mani

Steps to create widget

创建小部件的步骤

<?php
//protected/components
    class Categorywidget extends CWidget
    {

        public function init(){

        }

        public function run(){
            $model=Category::model()->findAll(array("condition"=>"isactive=1"));
            $listdata=CHtml::listData($model,"category_id","name");
            $this->render("category/category",array('listdata'=>$listdata));
        }

    }




 <?php
//protected/components/views
    $count=count($listdata);
    $div2=ceil($count/2)+1;
    $i=0;
    ?>
                <div class="categories">
                    <h5>Categories</h5>
                    <ul>
    <?php           foreach($listdata as $key=>$value) {
                        $i++;
                        if($div2==$i){ ?>
                        </ul><ul>    
    <?php               } ?>                
                        <li><a href="<?php echo Yii::app()->createAbsoluteUrl("medialist/".$this->REQUEST,array("category"=>$key)); ?>" class="colr"><?php echo $value; ?></a></li>
    <?php           } ?>
                    </ul>
                </div>

回答by Onkar Janwa

It's easy to create a widget in Yii, Just follow the link. Create Yii Widget

在 Yii 中创建小部件很容易,只需点击链接即可。创建 Yii 小部件

回答by Almas Adilbek

In 2 minutesI've understand how widgets work in this very short tutorial:
How to create Breadcrumb widget

2 分钟内,我已经在这个非常简短的教程中了解了小部件的工作原理:
如何创建面包屑小部件