简体   繁体   中英

Eloquent pagination not working with Slim 3 framework

I've build an costom mvc system base on Alex's Gareet "Authentication with Slim 3" course. It works great exept for the eloquent pagination, if i do something like $users = User::paginate(10); i get back exactly what i would expect. The problem comes when I try to render out the paginaton links by using the $users->links(); or the $users->render(); i get the fallowing error:

Type: Error Message: Call to a member function make() on null File: C:\\Users\\Andy\\Google_Drive\\wamp\\www\\proline\\new_admin\\vendor\\illuminate\\pagination\\LengthAwarePaginator.php Line: 90

After extensive Google search, I found out tnat i'm not the only one having this problem but I culd not find anyone offering a clear solution exept creating my own rendering functionality which i'm realy trying to avoid if i can, so i proceeded to reverse engineer the eloquent library and from what i can undestand, the problem seams to be Illuminate\\Support\\ServiceProvider\\PaginationServiceProvider class that has the methods "boot" and "register" which seem to be responsable with setting of the pagination view, but the whole class is not getting instaceated. To test this I did an die statement containg a string both in boot and register methods and nothing changed, I've tried to "new up" an instance of the PaginationServiceProvider class and I got a new error saying that the constructor of the parent class "ServiceProvider" expects an new instace of \\Illuminate\\Contracts\\Foundation\\Application as his argument. I have no idea which class implements this contract. Then I thought that I needed to register this class with slim as a service and i've tried the standard way to register servicess with slim

$container['esp'] = function ($container) use($capsule) { return new Illuminatee\\Events\\EventServiceProvider($container); };

$container['dsp'] = function ($container) use($capsule) { return new Illuminatee\\Database\\DatabaseServiceProvider($container); };

witch did nothing.

I'm lost an desperate for some help!

In case you are still struggling, the answer is here: https://codecourse.com/watch/slim-3-pagination

Basically you have to composer require illuminate/pagination and then build a ViewFactory class that implements the make() method used by the LengthAwarePaginator , then globally register:

```php

\Illuminate\Pagination\LengthAwarePaginator::viewFactoryResolver(function () {
            $paths = [
                config()->get('app.settings.paths.landers'),
                config()->get('app.settings.paths.views')
            ];

            $settings = [
                'cache' => config()->get('app.settings.paths.cache.views'),
                'debug' => config()->get('app.debug') // this also auto reloads views cache if set to true
            ];
            return new \Klever\Views\ViewFactory($paths, $settings);
        });

        \Illuminate\Pagination\LengthAwarePaginator::defaultView('partials/pagination.twig');

        \Illuminate\Pagination\Paginator::currentPathResolver(function () {
            return isset($_SERVER['REQUEST_URI']) ? strtok($_SERVER['REQUEST_URI'], '?') : '/';
        });

        \Illuminate\Pagination\Paginator::currentPageResolver(function () {
            return $_GET['page'] ?? 1;
        });

```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM