Mindexperiment
5 months ago
126
13
Laravel

Interfaces again

Posted 5 months ago by Mindexperiment

Hi programmers,

I just want to implement a simple feature, use an interface to manage 'em all!

So I wrote my interface

<?php

namespace App\Contracts;

interface Publishable {

 /**
  * The model can be published
  */
 public function publish();

 /**
  * The model can be unpublished
  */
 public function unpublish();

}

really simple :smile:, than implement the interface on a model

<?php

namespace App;

use App\Contracts\Publishable;

class Article implements Publishable {}

then in a controller I want to manage all models that can be publishable:

<?php

namespace App\Http\Controllers;

use App\Contracts\Publishable;

class PublisherController extends Controller {

 public function publish( Request $request, Publishable $model ) {
  $model->publish();

  return back();
 }
}

That's pretty simple task, by looking the docs I see that IOC needs to be registered before using the interface, so I register under AppServiceProvider:

<?php

// - AppServiceProvider

public function register()
    {
        $this->app->bind( Publishable::class, Article::class );
        $this->app->bind( Publishable::class, Comment::class );
    }

The binding doesn't recognize Article class but only Comment class, how can I assign a common interface to many models?

Please sign in or create an account to participate in this conversation.