title: Laravel: dynamic routes with FormRequest
body: <p>I have a system that might have a lot of controllers in the future. What I wanted to achieve (and achieved) is to make some form of dynamic routes, in order not to flood <code>routes/web.php</code> file with tons of route groups (there might be more than 300 routes).</p>
  <p>So I did this:</p>
  <p><code>routes/web.php</code>:</p>
  <pre><code>Route::get('/report/{report_name}', [ReportController::class, 'index'])-&gt;name('report');
  Route::post('/report/{report}/{method_name}', [ReportController::class, 'getRpcCall'])-&gt;name('report'); // This one used for RPC call (like Vue.js/AJAX calls)
  </code></pre>
  <p><code>ReportController.php</code>:</p>
  <pre><code>public function index(string $reportName = null)
  {
      if (!class_exists(self::REPORTS_NAMESPACE . $reportName . 'Controller')) {
          throw new ReportClassNotFoundException($reportName);
      }
  
      $report = app(self::REPORTS_NAMESPACE . $reportName . 'Controller');
  
      return $report-&gt;index();
  }
  
  public function getRpcCall(Request $request, string $report = null, string $methodName = null): mixed
  {
      if (!class_exists(self::REPORTS_RPC_NAMESPACE . $report . 'RpcController')) {
          throw new ReportClassNotFoundException($report);
      }
  
      $report = app(self::REPORTS_RPC_NAMESPACE . $report . 'RpcController');
      $method = $methodName;
  
      if (!method_exists($report, $method)) {
          throw new MethodNotFoundException($report, $methodName);
      }
  
      return $report-&gt;$method($request);
  }
  </code></pre>
  <p>But since I am a big fan of using FormRequests (in order not to bloat Controller with stuff that shouldn't be there, according to the SRP), I created a controller for reporting, that is being called dynamically, using above methods, like this: <code>{{ route('rpc.report', ['FuelReporting', 'updateFuelCardData']) }}</code>. Here it is:</p>
  <pre><code>class SomeReportingRpcController extends Controller
  {
    public function updateFuelCardData(SomeModelUpdateRequest $request): Response
    {
      // Some stuff going on here
    }
  }
  </code></pre>
  <p>As you can see, I use <code>SomeModelUpdateRequest</code> as a form request (created via <code>php artisan make:request SomeModelUpdateRequest</code>) for validation purposes. But the problem is that Laravel throws an error that</p>
  <pre><code>Argument #1 ($request) must be of type App\Http\Requests\SomeModelUpdateRequest, Illuminate\Http\Request given, called in ReportController.php
  </code></pre>
  <p>As I thought, that even if SomeModelUpdateRequest extends FormRequest (which extends Illuminate\Http\Request class), it should be fine, since method param type hinting is not conflicting with parent and child classes, but I was wrong.</p>
  <p>Any ideas on how to keep the &quot;dynamic route&quot; logic and be able to use Form Requests?</p>
  <p>I already have a working solution to ditch the Form request and use <code>$request-&gt;validate()</code> in some service class, but it is not so clean and I won't be able to use <code>$request-&gt;validated()</code> after that, that is kind of crucial for me.</p>
  <p>Kind regards,</p>