Support the ongoing development of Laravel.io →
posted 11 years ago
IOC

Hello,

Im facing some trouble trying to bind some classes dinamically in my Route.

I have the current Routes:

Route::resource('post', 'PostController');
Route::resource('comment', 'CommentController');

everything normal untill here, my problem is:

Route::post('post.like', 'LikeController');
Route::post('comment.like', 'LikeController');

so i can call /posts/1/like and /comment/1/like and use the same controller 'LikeController' in diferents contexts

I want to bind the interface dinamically

 App::Bind('LikeableInterface',

.. but need to fill it dinamically from the route to construct the controller.

class LikeController extends BaseControler
{
           public function __construct(LikeableInterface $resource
           {
             [...]

The Likeable can be an Comment or a Post, depending of the route. Anyone know how to achieve this in Laravel?

Thank You.

Last updated 3 years ago.
0

Sounds to me like you are talking about Traits. I'm currently reading about this new feature to php>=5.4, so i can point you to couple resources, but i can't give you code that will solve your problem.

Blog post 1

PHP Site

But in my understanding you can extract that 'like' feature to trait and then you can add that trait to any number of controllers.

Last updated 3 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.

© 2025 Laravel.io - All rights reserved.