php - PHP - 如何将一个索引函数用于两个不同的控制器?

50 2

我想在Laravel 5.8中编写像facebook这样的社交媒体网站。我在/home和/profile中有相同的朋友列表,但是有两个不同的控制器(HomeController ProfileController ),我不想两个不同控制器复制同一代码,我该如何解决它,我只需要使用代码一次?

HomeController.php


public function index()


 {


 $friends = [];


 $friendsID = Person_has_person::select('person2')->where('person1', Auth::user()->id)->get();


 foreach ($friendsID as $friendID)


 $friends[] = User::find($friendID->person2);



 return view('home')->with('friends', $friends);


 }



ProfileController.php


public function index()


 {


 $friends = [];


 $friendsID = Person_has_person::select('person2')->where('person1', Auth::user()->id)->get();


 foreach ($friendsID as $friendID)


 $friends[] = User::find($friendID->person2);



 return view('profile')->with('friends', $friends);


 }




我的web.php文件里有 route。


Route::get('/home', 'HomeController@index');


Route::get('/profile', 'ProfileController@index');




@if(count($friends) > 0)


 @foreach($friends as $friend)


 <ul>


 <li>


 <a href="/profile/{{$friend->id}}">{{$friend->firstname . ' ' . $friend->lastname}}</a>


 <a class="text-danger"


 href="/removeFriend/{{Auth::user()->id . '/' . $friend->id}}">[unfriend]</a>


 </li>


 </ul>


 @endforeach


@else


 <p>No friends available</p>


@endif



就像你看到的,两个不同的控制器中有相同的代码,这不是一个好的解决方案。

时间: 原作者:

82 3

尝试从其他controller调用index方法:


use ProfileController;



class HomeController {


 public function index()


 {


 ProfileController::index();


 }


}



原作者:
138 4

视图对于这种情况非常适合,你可以在方法中注册Composer,也可以创建自己的服务提供程序,并将它注册到config/app.phpproviders数组。


View::composer(


 ['home', 'profile'],


 'AppHttpViewComposersYourViewComposer'


);



compose方法共享相同的代码:


/**


* Bind data to the view.


*


* @param View $view


* @return void


*/


public function compose(View $view)


{


 $friends = [];


 $friendsID = Person_has_person::select('person2')->where('person1', Auth::user()->id)->get();



 foreach ($friendsID as $friendID)


 {


 $friends[] = User::find($friendID->person2);


 }



 $view->with('friends', $friends);


}



原作者:
61 5

为什么你不让两个不同的Routes指向同一个控制器动作?

比如

Route::get('/home', 'ProfileController@index'); Route::get('/profile', 'ProfileController@index');

原作者:
126 4

例如:

FriendRepository


<?php



namespace AppRepositories;



class FriendRepository


{


 public function getAll($user_id)


 {


 $friends = [];


 $friendsID = Person_has_person::select('person2')


 ->where('person1', $user_id)


 ->get();


 foreach ($friendsID as $friendID) {


 $friends[] = User::find($friendID->person2); 


 }



 return friends;


 }


}



然后在控制器里:

HomeController.php


<?php



namespace AppHttpControllers;



use AppRepositoriesFriendRepository;


use IlluminateHttpRequest;



class HomeController extends Controller


{


 /**


 * @var FriendRepository


 */


 private $friend_repository;



 public function __construct(FriendRepository $friend_repository)


 {


 $this->friend_repository = $friend_repository;


 }



 public function index()


 {


 $friends = $this->friend_repository->getAll(Auth::user()->id);



 return view('home')->with('friends', $friends);


 }


}



ProfileController.php


<?php



namespace AppHttpControllers;



use AppRepositoriesFriendRepository;


use IlluminateHttpRequest;



class ProfileController extends Controller


{


 /**


 * @var FriendRepository


 */


 private $friend_repository;



 public function __construct(FriendRepository $friend_repository)


 {


 $this->friend_repository = $friend_repository;


 }



 public function index()


 {


 $friends = $this->friend_repository->getAll(Auth::user()->id);



 return view('home')->with('friends', $friends);


 }


}



原作者:
...