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

Create multiple register functions like

public function registerUserRepo() {....}
public function registerComplaintRepo()  {....}

and then call these functions in main register function

Last updated 2 years ago.
0

Tried this way:


<?php

namespace Storage;
 
use Illuminate\Support\ServiceProvider;
 
class StorageServiceProvider extends ServiceProvider {

    public function register()  {
        registerUserRepo();
        registerComplaintRepo();
        registerComplaintRepo();
    }

    public function registerUserRepo() {
        return $this->app->bind('Storage\User\UserRepository', 'Storage\User\EloquentUserRepository');
    }

    public function registerComplaintRepo() {
        return $this->app->bind('Storage\Complaint\ComplaintRepository', 'Storage\Complaint\EloquentComplaintRepository');
    }

    public function registerComplaintRepo() {
        return $this->app->bind('Storage\Category\CategoryRepository', 'Storage\Category\EloquentCategoryRepository');
    }
}

thew me an error: Cannot redeclare Storage\StorageServiceProvider::registerComplaintRepo()

What i am doing wrong? (sorry for being noob)

Last updated 2 years ago.
0

You have the last function name wrong. Should be registerCategoryRepo not registerComplaintRepo (which is a redeclare of already declared function)

Last updated 2 years ago.
0

TorchSK said:

You have the last function name wrong. Should be registerCategoryRepo not registerComplaintRepo (which is a redeclare of already declared function)

lol, fixed that. Now i am getting this error: Call to undefined function Storage\registerUserRepo()

Last updated 2 years ago.
0

Call the functions in $this context in register function

public function register()  
{
    $this->registerUserRepo();
    $this->registerComplaintRepo();
    $this->registerCategoryRepo();
}

And I think the return in

return $this->app->bind(...)

is unnecessary

Last updated 2 years ago.
0

It works! Thank you. Was checking laravel docs about IoC container, but didn't found info about multiple bindings :(

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