How can I share data from a different controller i

2019-07-25 18:29发布

I have a news controller and a pages controller. The pages controller has a method called home, which returns the view for the home page. I want to be able to display this news module on the home page.

From the pages controller, how can I call upon the data from the news controller to display a news module on the front page?

标签: php oop laravel
1条回答
男人必须洒脱
2楼-- · 2019-07-25 19:04

You can create static method in news controller which returns news items and call it from pages controller, for instance:

class PagesController extends BaseController(){

 public function home(){
 $news = NewsController::GetNews();

 return View::make('home')
 ->with('news', $news);
 }
 }

class NewsController extends BaseController(){

public static function GetNews(){
 $news = NewsItem::OrderBy('id', 'DESC')->take(10)->get();
 return $news;
}

 }
查看更多
登录 后发表回答