composer require illuminate/database 和 illuminate/paginationdadang'd单独使用

单独使用illuminate//database 和illuminate/pagination

[导语]illuminate/database和illuminate/pagination作为laravel的组件。其实也可以单独拿出来使用。

database

composer require illuminate/database:5.4.36(tips:不指定版本默认使用最新的5.6.,需要php7.支持,最新版本使用了一些php7特征)

        //初始化
        $capsule = new Capsule;
        $capsule->addConnection([
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'site_km',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ]);
        //get the single;
        $capsule->setAsGlobal();
        $capsule->bootEloquent();

就可以使用database底下的Manager来构建查询了

pagination

composer require illuminate/pagination: 5.4.36

分页查询语句

$result=Capsule::table("test")
            ->select("id")
            ->orderByDesc('id')
            ->paginate(10);

在vendro\illuminate\pagination底下有个providerserver类,提供pagination的启动和注册信息的

 Paginator::viewFactoryResolver(function () {
            return $this->app['view'];
        });

        Paginator::currentPathResolver(function () {
            return $this->app['request']->url();
        });

        Paginator::currentPageResolver(function ($pageName = 'page') {
            $page = $this->app['request']->input($pageName);

            if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {
                return $page;
            }

            return 1;
        });

想单独使用就要初始化Paginator的一些信息

  • Paginator::viewFactoryResolver 可以自定义分页样式。需要返回一个重写了make方法的use Illuminate\Contracts\View\Factory接口的类
 public function make($view, $data = [], $mergeData = [])
    {
        $pagination=$data["paginator"];
        $elment=$data["elements"];
        $html=$this->bootstrapStyle($pagination,$elment);
        return new PaginationView($html,$view);

    }

$view是指定的渲染文件,$data包含paginator实例和elements实例,elements是一组包含页和url的数组。make方法返回重写了render方法的Illuminate\Contracts\View\View接口类

 public function render()
    {
        // TODO: Implement render() method.
        return $this->html;
    }

这样就可以渲染出一串html字符.

  • Paginator::currentPathResolver是解决路由使用的(例如thinkphp中按照module/controler/mentho加载),在这个方法你可以自己拼接请求参数。不配置默认为?/page=x
  $scheme=$_SERVER['REQUEST_SCHEME'];
          Paginator::currentPathResolver(function (){
          $scheme=$_SERVER['REQUEST_SCHEME'];
          $domain=$_SERVER['HTTP_HOST'];
          $requestUri="/index.php?m=".$_REQUEST["m"]."&a=".$_REQUEST["a"];
          $currentUrl=$scheme."://".$domain.$requestUri;
          return $currentUrl;
      });
  • Paginator::currentPageResolver这个方法返回当前的页数,也可以做一些非法检查等功能.
Paginator::currentPageResolver(function ($pageName="page"){
          if(isset($_REQUEST[$pageName])){
              $page=$_REQUEST[$pageName];
              if(filter_var($page,FILTER_VALIDATE_INT)!==false && (int) $page>0){
                  return $page;
              }
          }
          return 1;


      });

最后Paginator::currentPageResolver和Paginator::currentPathResolver需要在查询语句之前初始化
完整代码:

      Paginator::viewFactoryResolver($this->factoryResolver());
      Paginator::currentPageResolver($this->pageResolver());
      Paginator::currentPathResolver($this->pathResolver());
      $memberModel=new MemberModel();
      $result=$memberModel->memberList();

题外话

使用Comper中的ClassLoader加载类

 $loader = new \Composer\Autoload\ClassLoader();
        $loader->addPsr4("Modules\\", __DIR__."../Modules",true);
        // activate the autoloader
        $loader->register();
        // to enable searching the include path (eg. for PEAR packages)
        $loader->setUseIncludePath(true);

其中addPsr4方法中需要传入命名空间和时间的项目路径,命名空间和项目的路径就对应起来就可以加载使用到的项目文件

你可能感兴趣的:(php)