我有两个实体:用户,公告
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.
更多推荐
发布评论