IMPORTANT: Per accedir als fitxer de subversion: http://acacha.org/svn (sense password). Poc a poc s'aniran migrant els enllaços. Encara però funciona el subversion de la farga però no se sap fins quan... (usuari: prova i la paraula de pas 123456)

See:

SOLID_principles_example_with_Laravel

Implements Dependency Injection in constructor changing InvoiceController class to:

<?php namespace SolidLaravel\Http\Controllers;

use SolidLaravel\Http\Requests;
use SolidLaravel\Http\Controllers\Controller;

use Illuminate\Http\Request;
use SolidLaravel\InvoiceReport;
use SolidLaravel\Output\Contracts\InvoiceShowInterface;
use SolidLaravel\Output\InvoiceShowHtml;
use SolidLaravel\Output\InvoiceShowText;
use SolidLaravel\Repositories\Contracts\InvoiceRepositoryInterface;
use SolidLaravel\Repositories\InvoiceRepositoryDB;
use SolidLaravel\Repositories\InvoiceRepositoryFile;

class InvoiceController extends Controller {

    /**
     * @var InvoiceRepositoryInterface
     */
    private $repo;
    /**
     * @var InvoiceShowInterface
     */
    private $output;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(InvoiceRepositoryInterface $repo, InvoiceShowInterface $output)
    {
        $this->middleware('auth');
        $this->repo = $repo;
        $this->output = $output;
    }

    /**
     * Show invoice
     *
     * @return Response
     */
    public function index()
    {
        $invoice = new InvoiceReport($this->repo,1);
        return view('invoice')->with('totalAmmount',$invoice->show($this->output));

    }

}

Go to URL:

http://solid_laravel.app/invoice

And a BindingResolutionException will occurs. That's because No binding of Interfaces to especific classes is specified at Laravel Service Container. You could solve that with adding:

App::bind('SolidLaravel\Repositories\Contracts\InvoiceRepositoryInterface','SolidLaravel\Repositories\InvoiceRepositoryDB');
App::bind('SolidLaravel\Output\Contracts\InvoiceShowInterface','SolidLaravel\Output\InvoiceShowHtml');

to routes.php file but i will suggest you a better way. Let's create a Service Provider at folder app/Services (file app/Provicers/. See [1])

$ php artisan make:provider InvoiceServiceProvider
<?php namespace SolidLaravel\Providers;

use Illuminate\Support\ServiceProvider;

class InvoiceServiceProvider extends ServiceProvider {

	/**
	 * Bootstrap any application services.
	 *
	 * @return void
	 */
	public function boot()
	{
		//
	}

	/**
	 * Register any application services.
	 *
	 * This service provider is a great spot to register your various container
	 * bindings with the application. As you can see, we are registering our
	 * "Registrar" implementation here. You can add your own bindings too!
	 *
	 * @return void
	 */
	public function register()
	{
                // App::bind('SolidLaravel\Repositories\Contracts\InvoiceRepositoryInterface','SolidLaravel\Repositories\InvoiceRepositoryDB');
                // App::bind('SolidLaravel\Output\Contracts\InvoiceShowInterface','SolidLaravel\Output\InvoiceShowHtml');
		$this->app->bind(
			'SolidLaravel\Repositories\Contracts\InvoiceRepositoryInterface',
			'SolidLaravel\Repositories\InvoiceRepositoryDB'
		);

                $this->app->bind(
			'SolidLaravel\Output\Contracts\InvoiceShowInterface',
			'SolidLaravel\Output\InvoiceShowHtml'
		);
	}

}

And add Laravel Service Provider to Laravel App config. Edit file config/app.php ant add to providers:

'SolidLaravel\Providers\InvoiceServiceProvider',