Online Hotel Booking project - Need some logic building tips to prevent code duplication
Project: Online Hotel Booking portal
Page: User dashboard. Here user can add property and details (basic details, rooms, amenities, etc.)
Problem :
In the User Dashboard page, we ave several pages (Basic details, policies, room details, images, etc). One controller for each controller. For each controller, I am using a middleware VerifyPropertyUserMiddleware. Now I require fetching Property model in every single controller.
$ppt = PropertyMaster::findOrFail($pID);
Solutions that I can think of
Create this instance in middleware and pass it to the controller. (but I think middlewares are not supposed to do this job)
Create a trait, inside that trait create a method getProperty (but code duplication is still a problem as I will have to call the method in every controller)
Use sessions. But still, code duplication is still there because in every controller I will have to check whether the laravel session has $ppt variable or not.
class BaseController {
protected $ppt = null;
public function __construct(Request $request)
{
if ($isset(request->pID)
$this->ppt = PropertyMaster::findOrFail($request->pID);
//use request or session
}
}
class SubController {
public function __construct {
parent::__construct();
}
// use $this->ppt since it is inherited from base controller
}