如何更改Yii中的默认分页属性?

如何更改Yii中的默认分页属性?,yii,pagination,Yii,Pagination,在我的Yii项目中,我希望自动获取分页的默认页面大小,因此我不必在所有使用分页的小部件中指定它。但我似乎找不到一种方法来全局更改分页类,而不编辑Yii源文件。这可能吗?请使用以下代码在/components/WidgetFactory.php上创建文件 <?php /** * Custom WidgetFactory class * Provides two new events: * - onBeforeCreateWidget * - onAfterCreateWidge

在我的Yii项目中,我希望自动获取分页的默认页面大小,因此我不必在所有使用分页的小部件中指定它。但我似乎找不到一种方法来全局更改分页类,而不编辑Yii源文件。这可能吗?

请使用以下代码在/components/WidgetFactory.php上创建文件

<?php

/**
 * Custom WidgetFactory class
 * Provides two new events:
 *  - onBeforeCreateWidget
 *  - onAfterCreateWidget
 *
 * Allows for advanced global widget alteration, going a step further than CWidgetFactory's
 * typical process which allows you to define default values for widgets.
 *
 */
class WidgetFactory extends CWidgetFactory
{

    /**
     * Raised right BEFORE a widget is created.
     * @param CEvent $event the event parameter
     */
    public function onBeforeCreateWidget(CEvent $event)
    {
        $this->raiseEvent('onBeforeCreateWidget',$event);
    }

    /**
     * Raised right AFTER a widget is created.
     * @param CEvent $event the event parameter
     */
    public function onAfterCreateWidget(CEvent $event)
    {
        $this->raiseEvent('onAfterCreateWidget',$event);
    }

    /**
     * Creates a new widget based on the given class name and initial properties.
     * @param CBaseController $owner the owner of the new widget
     * @param string $className the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache)
     * @param array $properties the initial property values (name=>value) of the widget.
     * @return CWidget the newly created widget whose properties have been initialized with the given values.
     */
    public function createWidget($owner,$className,$properties=array())
    {
        if (! ($this->hasEventHandler('onBeforeCreateWidget') || $this->hasEventHandler('onAfterCreateWidget')))
            return parent::createWidget($owner, $className, $properties);

        $event=new WidgetEvent($this, $owner, $className, $properties);
        if ($this->hasEventHandler('onBeforeCreateWidget'))
            $this->raiseEvent('onBeforeCreateWidget', $event);
        $event->widget=parent::createWidget($owner, $className, $properties);
        if ($this->hasEventHandler('onAfterCreateWidget'))
            $this->raiseEvent('onAfterCreateWidget', $event);
        return $event->widget;
    }

}

class WidgetEvent extends CEvent
{
    /**
     * @var CBaseController Owner of the new widget
     */
    public $owner;

    /**
     * @var string Widget class name
     */
    public $className;

    /**
     * @var CWidget The newly created widget
     */
    public $widget;

    /**
     * Constructor.
     * @param WidgetFactory $sender The WidgetFactory instance
     * @param CBaseController $owner The owner of the new widget
     * @param string $className The class name of the widget. This can also be a path alias.
     * @param array $params The initial property values (name=>value) of the widget.
     */
    public function __construct(WidgetFactory $sender, CBaseController $owner, $className, array $params=array())
    {
        parent::__construct($sender, $params);
        $this->owner=$owner;
        $this->className=$className;
    }
}

请注意配置代码上面的默认页面大小。我想这会解决你的问题。

这正是你需要的!请将你的要点复制到答案中,不要只是链接到要点。如果你删除了要点,答案就没有意义了
return array(
    // ...
    'components'=>array(
        // ...
        'widgetFactory'=>array(
            'class'=>'WidgetFactory',
            'onAfterCreateWidget'=>function(WidgetEvent $event){
                static $defaultPageSize=50; // YOUR_DEFAULT_PAGESIZE_HERE
                $widget=$event->widget;
                if ($widget instanceof CBaseListView) {
                    /** @var CBaseListView $widget */
                    if ($widget->dataProvider!==null && $widget->dataProvider->pagination!==false)
                        $widget->dataProvider->pagination->pageSize=$defaultPageSize;
                }
            },
        ),
        // ...
    ),
);