简体   繁体   中英

Laravel 7/8 best practice for multipart page

Im very new to Laravel. It seems there are a few ways to get things done so Im looking for best practice on multipart page load.

In php I might have loaded all I need in a function and passed all my objects/results as vars then use the vars as needed on the page.

  • this has the drawback of a large function plus individual functions for refreshing part of the content with AJAX

Here I send all vars to page on load, it returns view, so I cant use this function later for page part

 // load full page with vars for parts 
 // ( will require other functions for separate requests later )
 public function profilepage(){
   // do query set result as var to be passed
   // do query set result as var to be passed
   // do query set result as var to be passed
 return view('profiles.profile', ['data'=>$data,'social'=>$social,'exper'=>$experience]);
 }

With Laravel, we have routes that are pretty flexible to get exactly what we need.

  • so it seems I would want to set all my page parts as routes to individual functions on Load
  • then use those same functions for refresh with AJAX?

Would this load the page while also loading the page parts? Is the best way to multi-part content?

// load page
Route::get('/profile', 'ProfileController@profile');
// get page parts
Route::get('/profile', 'ProfileController@profile')->('get.social_content');
Route::get('/profile', 'ProfileController@profile')->('get.job_content');
Route::get('/profile', 'ProfileController@profile')->('get.experience_content');

In my honest opinion a single query param on a single endpoint would to the trick

Route::get('profile/{param}', 'ProfileController@profile');

then

public function profile(string $param) {
 if (!$param) {} // default action + return

  //any other logic
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM