Extend Request class in Laravel 5

后端 未结 6 1415
猫巷女王i
猫巷女王i 2020-12-08 22:48

I\'m new to Laravel (only experienced Laravel 5, so no legacy hang up here)

I\'d like to know how to extend the core Request class. In addition to how to extend it,

6条回答
  •  余生分开走
    2020-12-08 22:58

    Yerkes answer inspired me to write a custom class, for use with pagination, but only on specific requests

    page() - 1) * $this->perPage();
        }
    }
    

    I then also had to register a new ServiceProvider in /config/app.php, which looks like

    app->resolving(PaginatedRequest::class, function ($request, $app) {
                PaginatedRequest::createFrom($app['request'], $request);
            });
        }
    }
    

    Now I can simply inject the PaginatedRequest in my controller methods only when I need it

    page();
            // ...
        }
    }
    

提交回复
热议问题