Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中嵌套控制器和视图_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 在Laravel中嵌套控制器和视图

Php 在Laravel中嵌套控制器和视图,php,laravel,laravel-4,Php,Laravel,Laravel 4,我的Laravel项目中有一个名为ImageController的控制器。这是一个非常基本的CRUD控制器 当我通过我的ImageController@show操作,我还想显示注释。但是,我不想将注释逻辑放在我的ImageController中。对于这个逻辑,我创建了一个ImageCommentController 我真的不知道该怎么做,但我正试图做这样的事情: class ImageController extends BaseController { // methods ...

我的Laravel项目中有一个名为
ImageController
的控制器。这是一个非常基本的CRUD控制器

当我通过我的
ImageController@show
操作,我还想显示注释。但是,我不想将注释逻辑放在我的
ImageController
中。对于这个逻辑,我创建了一个
ImageCommentController

我真的不知道该怎么做,但我正试图做这样的事情:

class ImageController extends BaseController {
    // methods ...

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

很抱歉,如果措辞含糊不清,请告诉我是否是这样,我会尽量让它更容易理解。

可能比使用控制器显示注释更好的解决方案是使用一个类,该类带有renderComments()方法,该方法基本上执行以下操作:

class Comments {
    public static renderComments($commentType = 'images')
    {
        $comments = Comments::where('comment_type', '=', $commentType)->get();
        return View::make('comments', $comments)->render();
    }
}
然后,例如在图像视图中:

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

这正是我想要的。非常感谢。为什么不将代码放入
图像控制器中?这就是控制器的作用——收集数据以传递给视图。我不同意您在下面选择的解决方案,因为这似乎是一项不必要的工作。我不知道我到底想把注释放在哪里,所以我想将注释逻辑与
ImageController
分离。