Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Implicit route model binding a binded interface #11791

Closed
eklundkristoffer opened this issue Jan 9, 2016 · 3 comments · Fixed by #40822
Closed

[5.2] Implicit route model binding a binded interface #11791

eklundkristoffer opened this issue Jan 9, 2016 · 3 comments · Fixed by #40822

Comments

@eklundkristoffer
Copy link

Hello, I try to do a implicit route model binding on a interface that is binded to my User model but it wont work. I created a thread on Laracasts about this, so you can see the example over there. https://laracasts.com/discuss/channels/laravel/implicit-route-model-bind-a-contract

EDIT: Adding the example here as well

// routes.php
$router->get('/implicit/{id}', function(App\Contracts\User $id){
    dd($id);
});
// AppServiceProvider .php
class AppServiceProvider extends ServiceProvider
{
    /**
     * Hold the bidnings
     *
     * @var array
     */
    protected $bindings = [
        'user' => [\App\Contracts\User::class => \App\User::class],
    ];

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

    /**
     * Register
     *
     * @return void
     */
    public function register()
    {
        if (! isset($this->bindings)) return;

        foreach ($this->bindings as $alias => $binding) {
            list($abstract, $concrete) = [key($binding), current($binding)];

            $this->app->bind([$alias => $abstract], $concrete);
        }
    }
}
// User.php
class User extends Authenticatable implements Contracts\User
{
    protected $table = 'players';
}
// Contracts/User.php
interface User
{

}

Is there a way to accomplish this? Thanks :)

@eklundkristoffer eklundkristoffer changed the title [Laravel 5.2] Implicit route model binding a binded interface [5.2] Implicit route model binding a binded interface Jan 9, 2016
@GrahamCampbell
Copy link
Member

Thanks you for your question, but I politely ask that questions do remain on the forums.

@fulopattila122
Copy link

fulopattila122 commented Jul 26, 2017

I'm also having this issue (type hinting with interface doesn't load the model). I dug in Laravel's source and I think I could implement it. The question is whether would that PR be accepted? If yes, I'll do it - would be a nice addition to 5.5 ;)

@krychu90
Copy link
Contributor

Still implicit model binding on interface is not working in Laravel 8.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants