还可以在另一个控制器Laravel中使用控制器吗?(As well to use controller inside another controller Laravel?)

编程入门 行业动态 更新时间:2024-10-22 23:43:37
还可以在另一个控制器Laravel中使用控制器吗?(As well to use controller inside another controller Laravel?)

我有两个实体:用户,公告

Eaach用户可以发布公告。

我创建了控制器AnnouncemetController ,其中有方法类: my() ,它返回当前用户的注释。

此外,我有控制器ProfileController ,代表当前的配置文件用户,我需要显示用户的所有公告。

为此,我尝试在ProfileController中重用控制器AnnouncemetController并调用public方法my() 。

使用App \ Http \ Controllers \ AnnouncementController;

class ProfileController extends Controller { $my = new AnnouncementController(); $my->my(); }

使用这样的好吗?

I have two entities: users, announcements

Eaach user can publish the announcements.

I creted controller AnnouncemetController, where there is method class: my(), that returns notes for current user.

Also I have controller ProfileController that represent current profile user, where I need to show all announcements of user.

For this I tried to reuse controller AnnouncemetController inside ProfileController and call public method my().

use App\Http\Controllers\AnnouncementController;

class ProfileController extends Controller { $my = new AnnouncementController(); $my->my(); }

Is it well to use such?

最满意答案

Laravel控制器将uri映射到动作。 在您的示例中,您使用控制器来访问数据,因此这不是“正确的事情”。

而是使用模型方法来访问数据。

A Laravel controller maps a uri to an action. In your example, you are using a controller to access data, so this is not the "right thing to do".

Instead use model methods to access the data.

更多推荐

本文发布于:2023-08-07 11:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   还可以   controller   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!