Support the ongoing development of →
posted 10 years ago
Last updated 2 years ago.

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

// 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 2 years ago.

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

You can use it to automatically inject the dependencies of your class. (

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:

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

Last updated 2 years ago.

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 2 years ago.

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 2 years ago.

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

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

Sign in to participate in this thread!


Your banner here too?


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

Your logo here?

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

© 2024 - All rights reserved.