### **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.
- Dependency Injection
- The Problem
- Build A Contract
- Take It further
- Too Much Java?
- The IoC Container
- Basic Binding
- Reflective Resolution
- Interface As Contract
- Strong Typing & Water Fowl
- A Contract Example
- Interface & Team Development
- Service Provider
- As Bootstrapper
- As Organizer
- Booting Providers
- Providing The Core
- Application Structure
- MVC Is Killing You
- Bye, Bye Models
- It's All About The Layers
- Where To Put "Stuff"
- Applied Architecture: Decoupling Handles
- Decoupling Handlers
- Other Handlers
- Extending The Framework
- Manager & Factories
- Cache
- Session
- Authentication
- IoC Based Extension
- Request Extension
- Single Responsibility Principle
- Open Closed Principle