will
will

Reputation: 1511

Nesting controllers and views in Laravel

I have a controller in my Laravel project called ImageController. This is a pretty basic CRUD controller.

When I access /images/{id} through my ImageController@show action, I want to also display comments. However, I don't want to put the comment logic in my ImageController. For this logic, I have created an ImageCommentController.

I'm not really sure how to go about this, but I'm trying to do something of this sort:

class ImageController extends BaseController {
    // methods ...

    public function show($id)
    {
        $images = // get images ...
        $this->layout->view = // images.show and imagescomment.index (using ImageCommentsController@index logic)
    }
}

I'm sorry if this is vaguely phrased, let me know if it is and I'll try to make it more understandable.

Upvotes: 0

Views: 127

Answers (1)

chris342423
chris342423

Reputation: 451

Maybe a better solutions than using a Controller for displaying the comments is to use a class with a method renderComments() that basically does something like:

class Comments {
    public static renderComments($commentType = 'images')
    {
        $comments = Comments::where('comment_type', '=', $commentType)->get();
        return View::make('comments', $comments)->render();
    }
}

Then for example inside your image view:

...
{{ Comments::renderComments() }}
...

Upvotes: 2

Related Questions