Support the ongoing development of Laravel.io →
posted 10 years ago
IOC
Last updated 1 year ago.
0

This will (hopefully) work for you (not sure about namespacing as name tho).

<?php
// class.php
namespace My;

class NewClass {
    function __construct($id, $title) 
    {
        $this->id = $id;
        $this->title = $title;
    }
}

// another file (etc global.php)
$classArgs = [
   'id'    => 1,
   'title' => 'test',
];

App::bind('My\NewClass', function() use ($classArgs) {
	return new My\NewClass($classArgs['id'], $classArgs['title']);
});

// get the binding
$newClass = App::make('My\NewClass');
Last updated 1 year ago.
0

In this case you don't need to use App::make()

You can use it to automatically inject the dependencies of your class. (http://laravel.com/docs/ioc#automatic-resolution)

Just a example:

class Importer {
     
     public function __construct(Filesystem $files, Sync $sync)
     {
           $this->files = $files;
           $this->sync = $sync;
     }

}

So, the IoC will auto inject the Filesystem and Sync classes (if they exist, of course) when you call App::make('Importer');

You can know more about it on the docs:

http://laravel.com/docs/ioc

If you want to use repository pattern in laravel, take a look at this tutorial:

http://culttt.com/2013/07/08/creating-flexible-controllers-in-...

Last updated 1 year ago.
0

Hey guys thanks for your reply. I am bit of a design pattern and Laravel noob (just a warning).

I haven't tested it yet but I think Marwelln's answer could be what I am looking for. I understand the IOC container can resolve dependencies that are type hinted, as explained in gabrielkoerich's answer. But I am currently trying to instantiate an object with data from a repository which is all primitive values.

I am not using Eloquent for my models so I was trying to work out which way is best to create objects. After reading the TutsPlus article, it suggested to use a Factory Pattern object which I was assuming was the App::make().

Last updated 1 year ago.
0

Hey TheoKouzelis, This reply is a bit late and you've probably sorted it out yourself - but maybe this will help someone else.

It sounds like you want to instantiate an object and inject the repository when the object is constructed?

I would do the following.

Let's say the repository is called UserRepository - remember, if you are coding to an interface you need to bind the specific implementation you'd like to use to the interface using App::bind()

In the class I want to use to instantiate the object I would put:

use path\to\UserRepository;

class ExampleClass
{ 
   
  protected $userRepo;

  public function __construct(UserRepository $userRepo)
   {
      $this->userRepo = $userRepo;
   }

Last updated 1 year ago.
0

maybe this will help someone else how to use App::make()

$foo = App::make('BaseController')->myAction( $param, $param2 );
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.