I've run into an error when upgrading my app to PHP 8.0 and I'm not sure how to fix it.
I have a route that loads a page which is generated for the current date by default, but can accept another month and day in the url params and load that instead. In my routes file this looks like:
$router->get('today/{month?}/{day?}', 'TodayController@index')->name('today.index');
Then in my TodayController I process that with the following:
public function index(int $month = null, int $day = null, DailystoryRepository $dailystoryRepository, EventRepository $eventRepository ) {
[$month, $day] = $this->prepareToday($month, $day); //calculates current month # and day #
$story = $dailystoryRepository->findForDate($month, $day);
$events = $eventRepository->findForDate($month, $day);
return view('today.index', compact('story', 'events'));
}
For the last 5 years this worked fine, but in php 8+ I get an error when I try to load the default page example.com/today (without the day and month parameters)
The error is as follows Argument #1 ($month) must be of type ?int, Chi\Platform\Repositories\DailystoryRepository given
What is the proper way to handle this?