mrsimonbennett/dipr

Dependency Injection For Controller Methods in Laravel
88 1
Install
composer require mrsimonbennett/dipr
PHP:>=5.4.0
License:MIT
Last Updated:Aug 7, 2014
Links: GitHub  ·  Packagist
Maintainer: mrsimonbennett

Dipr

Dependency Injection for Methods in Laravel Controllers

Enabling you to use the IOC to inject into controllers methods. Why? Well somethings you have a instance of something you only want for one of a controllers methods so does not make sense to inject via the constructor.

Quick Example

 public function index( \ACME\Validation\Company $companyValidation)
    {    }

#Requirments

  • Laravel 4.2
  • PHP 5.4
  • Changing the Base Controller/Your Controller to Extend from the packages

#Installing

Add To composer.json

{
 "require": {
   "mrsimonbennett/dipr": "dev-master"
 }
}

I recommend you use a BaseController of some sort, if not your controller need to extend Mrsimonbennett\Dipr\Controller

 <?php
use Mrsimonbennett\Dipr\Controller;
class BaseController extends Controller {
}

#Usage Example (needs more work)

/**
 * Class ProcessController
 * @package Amce\Controllers
 */
class ProcessController extends \BaseController
{

    /**
     * @param Request                $request
     */
    public function getRequest(Request $request)
    {
        return $request->All();
    }
     /**
     * @param Request                $request
     * @param string                 $slugFromRouteConfig
     */
    public function getRequestWithURLSlug(Request $request, $slugFromRouteConfig)
    {
    
    }
      /**
     * @param User                   $userFromRouteConfig
     * @param Request                $request
     * @param string                 $randomSlugStringFromRouteConfig
     */
    public function getRequestWithModelFromRouter(User $userFromRouteConfig,Request $request, $randomSlugStringFromRouteConfig)
    {
    
    }
}

As you can see objects are always first before strings/ints loaded using the router.php

#How It works

  1. The code looks at the signuture of the method.
  2. Then it looks at parameters the router is sending
  3. If the any of the parameters are objects there are stored in a list
  4. The code them matches the objects in the stored list with the signuture
  5. If the object in the signuture does not exist the use the App::Make() method is called, which will use the magic of the laravel's IOC to pass you that object (whether it neededs to be created or already exists)
  6. Anything thats not in the process like slugs and ints are passed to the method last.

If you get stuck you can always use var_dump the func_get_args() and see whats going on

Related Packages

reliese/laravel

Reliese Components for Laravel Framework code generation.

3,893,314 1,705
php-tmdb/laravel

Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the...

53,312 161
laravel-ja/laravel

The Laravel Framework.

5,667 17
kavenegar/laravel

laravel 4 and 5 kavenegar integration

361,233 86
laravel/laravel

The skeleton application for the Laravel framework.

64,400,768 84,869