🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### **IoC Based Extension** ### Almost every service provider included with the Laravel framework binds objects into the IoC container. You can find a list of your application's service providers in the ``app/config/app.php`` configuration file. As you have time, you should skim through each of these provider's source code. By doing so, you will gain a much better understanding of what each providers adds to the framework, as well as that keys are used to bind various services into the IoC container. For example, the ``PaginationServiceProvider`` binds a ``paginator`` key into the IoC container, which resolves into ``Illuminate\Pagination\Environment`` instance. You can easily extend and override this class within your own application by overriding this IoC binding. For example, you could create a class that extend the base ``Environment``: ~~~ <!-- lang:php --> namespace Snappy\Extensions\Pagination; class Environment extends \Illuminate\Pagination\Environment { // } ~~~ Once you have created your class extension, you may create a new ``SnappyPaginationProvider`` service provider class which overrides the paginator in its ``boot`` method: ~~~ <!-- lang:php --> class SnappyPaginationProvider extends PaginationServiceProvider { public function boot() { App::bind('paginator', function() { return new Snappy\Extensions\Pagination\Environment; } parent::boot(); } } ~~~ Note that this class extends the ``PaginationServiceProvider``, not the default ``ServiceProvider`` base class. Once you have extended the service provider, swap out the ``PaginationServiceProvider`` in your ``app/config/app.php`` configuration file with the name of your extended provider. This is the general method of extending any core class that is bound in the container. Essentially every core class is bound in the container in this fashion, and can be overridden. Again, reading through the included framework service providers will familiarize you with where various classes are bound into the container, and what keys they are bound by. This is a great way to learn more about how Laravel is put together.