php扩展分页类

Laravel 4.2官方默认提供的分页类,虽然对bootstrap添加了支持,但是很大的情况下不够用,这时候就需要对Laravel的Pagination进行简单的扩展了。
下面步骤中一、二、五、为关键的步骤,三、四可以参考官方文档
在新建的CodingInk目录下,其实就可以用来放扩展vender目录下的一些第三方库的方法,因为采用一、四、五的步骤后,它就被larave自动加载了。

一、app目录添加新目录

app/
├── CodingInk
│   └── Presenters
│   └── GeneralPresenter.php
├── commands
├── config
├── controllers
├── database
│   ├── migrations
│   └── seeds
├── filters.php
├── lang
├── models
├── routes.php
├── start
│   ├── artisan.php
│   ├── global.php
│   └── local.php
├── storage
│   ├── cache
│   ├── logs
│   ├── meta
│   ├── sessions
│   └── views
├── tests
└── views

这里的CodingInk目录可以自由定义

二、扩展Presenter的文件

GeneralPresenter.php文件的源码如下所示:

<?php
namespace CodingInk\Presenters;
use Illuminate\Pagination\Presenter as Presenter;
class GeneralPresenter extends Presenter {
public function getPageLinkWrapper($url, $page, $rel = null)
{
$rel = is_null($rel) ? '' : ' rel="'.$rel.'"';
return '<li><a href="' . $url . '" class=""' . $rel . '>' . $page . '</a></li>';
}
public function getDisabledTextWrapper($text)
{
return '<li class=""><a class="disabled">' . $text . '</a></li>';
}
public function getActivePageWrapper($text)
{
return '<li class="one"><a href="">' . $text . '</a></li>';
}
public function getPrevious($text = '上一页')
{
if ($this->currentPage <= 1)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl($this->currentPage - 1);
return $this->getPageLinkWrapper($url, $text, 'prev');
}
}
public function getNext($text = '下一页')
{
if ($this->currentPage >= $this->lastPage)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl($this->currentPage + 1);
return $this->getPageLinkWrapper($url, $text, 'next');
}
}
}

注意代码顶部的命名空间

三、定义新的模板文件
文件定义在:app/views/pagination/default.blade.php 中

<?php
$presenter = new CodingInk\Presenters\GeneralPresenter($paginator);
?>
<?php if ($paginator->getLastPage() > 1): ?>
<div class="right_bottom">
<ul>
<?php echo $presenter->render(); ?>
</ul>
</div>
<?php endif; ?>

___四、修改配置文件,使分页调用新的模板
修改app/config/view.php文件

<?php
return array(
'paths' => array(__DIR__.'/../views'),
'pagination' => 'pagination.default', //这里修改调用的默认模板
);

___五、自动加载CodingInk目录下的类

进行到1-4的步骤后,会发现页面会报:找不到CodingInk\Presenters\GeneralPresenter
所以需要进行以下几个步骤:

  1. 修改 app/start/global.php, 添加CodingInk目录至
ClassLoader::addDirectories(array(
app_path().'/CodingInk', //新添加的目录
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
  1. 修改composer.json文件,添加

    {
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
    "laravel/framework": "4.2.*",
    "intervention/image": "2.0.9"
    },
    "autoload": {
    "classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php",
    "app/CodingInk" //这里是新添加的
    ]
    },
    "scripts": {
    "post-install-cmd": [
    "php artisan clear-compiled",
    "php artisan optimize"
    ],
    "post-update-cmd": [
    "php artisan clear-compiled",
    "php artisan optimize"
    ],
    "post-create-project-cmd": [
    "php artisan key:generate"
    ]
    },
    "config": {
    "preferred-install": "dist"
    },
    "minimum-stability": "stable"
    }
  2. 重新生成自动composer的自加载文件

    php composer.phar dump-autoload