When you use composer packages (like a framework or a router library), they typically handle the underlying logic for routing, dependency injection (DI), middleware, and other essential features for you. You don’t need to write all of that logic yourself.
However, it’s still important to understand what each concept does:
- Router: Decides how to handle incoming requests and map them to the right controller or action.
- Dependency Injection (DI): Handles automatic injection of dependencies (like services or classes) into your controllers or other classes.
- DI Service Container: A container that holds all the services and manages their lifecycles (how they are created and passed around).
- Middleware: Allows you to add additional behavior or checks before your request reaches the final controller (e.g., authentication, logging).
- Group Middleware: Lets you apply multiple middlewares to a group of routes at once.
So, while packages automate the implementation, understanding how and why these concepts work helps you configure and extend them effectively.
Testing and experimenting with well-known Composer packages will help you understand the core concepts like routing, dependency injection, middleware, and service containers, which are central to frameworks like Laravel. Here are some well-known Composer packages for each of these concepts that you can try out with your custom PHP MVC framework:
1. Routing
Package: FastRoute
- What it does: FastRoute is a fast and simple routing library that maps HTTP requests to routes and handlers (controllers or functions). It’s a great package to experiment with routing in your custom framework.
- Why use it: It’s lightweight and flexible, and you can control how you define and handle routes.
Installation:
composer require nikic/fast-route
We used to demonstrate basic idea using illuminate routing package and a small working example is on GitHub.
2. Dependency Injection (DI)
Package: PHP-DI
- What it does: PHP-DI is a powerful Dependency Injection container that helps manage your services and their dependencies. It allows automatic injection and offers a simple interface to work with.
- Why use it: It gives you a deeper understanding of how a DI container works and how dependencies are resolved.
Installation:
composer require php-di/php-di
We used to demonstrate the basic idea using php-di package and a small example is on Github. Initially we successfully tried to generate object chaining of dependent classes using ReflectionClass. See how the logic is complex there. But if we initiated php-di composer, it automatically handles this behind the scene.
3. DI Service Container
Package: Pimple
- What it does: Pimple is a small, lightweight dependency injection container for PHP. It allows you to define services and resolve their dependencies.
- Why use it: It’s perfect for building your own container or learning how service containers work. It’s a good, simple container that still teaches you all the key concepts.
Installation:
composer require pimple/pimple
4. Middleware
Package: Middleware
- What it does: The Middlewares package offers various middleware components that you can use to process HTTP requests and responses. It’s a collection of commonly used middlewares like authentication, logging, CORS handling, etc.
- Why use it: It helps you experiment with adding middleware to your framework to control request flow (like logging, security checks, etc.).
Installation:
composer require middlewares/middlewares
5. Group Middleware
Package: Slim Framework (Slim 4)
- What it does: Slim is a micro-framework that supports middleware, including grouping middlewares. It’s a good package to learn how grouping middlewares work in larger applications.
- Why use it: It allows you to experiment with middleware groups, routing, and service containers. Even though it’s a full-fledged micro-framework, you can pick and use just the parts that interest you.
Installation:
composer require slim/slim
6. Request Handling & Validation
Package: Respect/Validation
- What it does: Respect/Validation provides a simple way to validate input data. It’s helpful when you want to handle form inputs, URL parameters, and JSON payloads in your framework.
- Why use it: You can learn how to manage input validation cleanly, and it integrates well into the middleware process (e.g., validating requests before reaching the controller).
Installation:
composer require respect/validation
7. Template Rendering (View Engine)
Package: Twig
- What it does: Twig is a flexible, fast, and secure template engine for PHP. It helps you separate business logic from presentation logic, a common practice in MVC frameworks.
- Why use it: It’s great for understanding how template rendering works and how you can integrate it into your custom PHP framework.
Installation:
composer require "twig/twig:^3.0"
8. Database ORM
Package: Eloquent ORM
- What it does: Eloquent is an ORM (Object-Relational Mapper) provided by Laravel. It’s a powerful tool for working with databases, turning database records into PHP objects.
- Why use it: Even if you’re not using Laravel, Eloquent is a great way to learn how ORM works. You can integrate it into your custom framework to see how data models, relationships, and querying work.
Installation:
composer require illuminate/database
9. Logging
Package: Monolog
- What it does: Monolog is a flexible logging library for PHP that supports different log handlers (e.g., files, databases, and email notifications).
- Why use it: Learning how logging works in a custom PHP framework helps you understand error tracking and debugging. It’s widely used in many PHP frameworks.
Installation:
composer require monolog/monolog
10. Authentication (Optional)
Package: OAuth 2.0 Server
- What it does: OAuth 2.0 Server is a package that helps you implement OAuth2 authentication in your custom PHP framework. It’s a bit advanced, but great for understanding security and authentication flows.
- Why use it: Understanding authentication mechanisms is key when learning frameworks like Laravel, especially with modern web app security practices.
Installation:
composer require league/oauth2-server
Conclusion
By experimenting with these packages, you’ll gain a deeper understanding of essential concepts like routing, middleware, dependency injection, and service containers—all of which are central to modern PHP frameworks like Laravel. Once you’re comfortable with these concepts, you’ll have a solid foundation for diving into more advanced features in Laravel.
There are many packages in composer. You can use anyone which is well-known and easy for you. So before jumping to Laravel, If you do trial & error with those would be a great booster for you to become a master of Laravel.
