title: Laravel 5.2 - Updated and Route group Middleware now is no longer called
body: <p>So I was on the latest Laravel 5.1 and updated to 5.2 today. In my routes I have something like this:</p>
  
  <pre><code>Route::group(['middleware' =&gt; ['api']], function() {
      // Members
      Route::get('members', '{api-namespace}\MembersController@index');
      Route::get('member/{id}', '{api-namespace}\MembersController@show');
  
      // Members Pension
      Route::get('member/{id}/pension/beneficiaries', '{api-namespace}\Inquiry\MembersPensionController@showBeneficiaries');
      Route::get('member/{id}/pension/contributions', '{api-namespace}\Inquiry\MembersPensionController@showContributions');
      Route::get('member/{id}/pension/yearlySummary', '{api-namespace}\Inquiry\MembersPensionController@showYearlySummary');
      Route::get('member/{id}/pension/pensioners', '{api-namespace}\Inquiry\MembersPensionController@showPensioners');
  
      // Members Summary
      Route::get('member/{id}/beneficiaries/{fund?}', '{api-namespace}\BeneficiariesController@showByMember');
      Route::get('member/{id}/contributions/', '{api-namespace}\ContributionsController@showByMember');
  
      // Beneficiaries
      Route::get('beneficiaries', '{api-namespace}\BeneficiariesController@index');
      Route::get('beneficiary/{id}', '{api-namespace}\BeneficiariesController@show');
  
      // Contributions
      Route::get('contributions', '{api-namespace}\ContributionsController@index');
  
      Route::get('users', '{api-namespace}\UsersController@index');
  });
  </code></pre>
  
  <p>The api-version middleware basically checks the header for an api-version and then fills in {api-namespace} in the route actions appropriately. This works fine in 5.1. However, since upgrading I get <code>Class App\\Http\\Controllers\\{api-namespace}\\MembersController does not exist</code> and it doesn't even hit my middleware at all. I have a feeling that they may have switched the order of the code so that it validates the route actions BEFORE running middleware because if I put the middleware in globally it works fine. However, I need this api-version group, so if anyone has any ideas how to get around this I am all ears.</p>
  
  <p>As per request:</p>
  
  <p>Kernel.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http;
  
  use Illuminate\Foundation\Http\Kernel as HttpKernel;
  
  class Kernel extends HttpKernel
  {
      /**
       * The application's global HTTP middleware stack.
       *
       * @var array
       */
      protected $middleware = [
          \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
          \App\Http\Middleware\EncryptCookies::class,
          \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
          \Illuminate\Session\Middleware\StartSession::class,
          \Illuminate\View\Middleware\ShareErrorsFromSession::class,
      ];
  
      /**
       * The application's route middleware.
       *
       * @var array
       */
      protected $routeMiddleware = [
          'auth' =&gt; \App\Http\Middleware\Authenticate::class,
          'auth.basic' =&gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
          'guest' =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
          'api-version' =&gt; \App\Http\Middleware\ApiVersionMiddleware::class,
      ];
  
      /**
       * The application's route middleware groups.
       *
       * @var array
       */
      protected $middlewareGroups = [
          'web' =&gt; [
              \App\Http\Middleware\EncryptCookies::class,
              \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
              \Illuminate\Session\Middleware\StartSession::class,
              \Illuminate\View\Middleware\ShareErrorsFromSession::class,
              \App\Http\Middleware\VerifyCsrfToken::class,
          ],
          'api' =&gt; [
              'throttle:60,1',
              'api-version',
              'cors'
          ],
      ];
  }
  </code></pre>
  
  <p>ApiVersionMiddleware.php</p>
  
  <pre><code>&lt;?php namespace App\Http\Middleware;
  
  use Closure;
  use Jbm\Exceptions\ApiVersionException;
  use Jbm\Helpers\ApiVersion;
  
  class ApiVersionMiddleware
  {
  
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          $route = $request-&gt;route();
          $actions = $route-&gt;getAction();
  
          $requestedApiVersion = ApiVersion::get($request);
          if (!ApiVersion::isValid($requestedApiVersion)) {
              throw new ApiVersionException('Invalid API Version');
          }
  
          $apiNamespace = ApiVersion::getNamespace($requestedApiVersion);
  
          $actions['uses'] = str_replace(
              '{api-namespace}', $apiNamespace, $actions['uses']
          );
  
          $route-&gt;setAction($actions);
  
          return $next($request);
      }
  }
  </code></pre>
  
  <p>Also notice that I moved my middleware into a middleware group and its still not working. Just an fyi</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I tried moving the middleware to global but at that point it has 0 information about the route which means that it can't modify the route to replace <code>{api-namespace}</code>. I believe the issue is that the route 'uses' is checked BEFORE middleware is run on it which obviously fails. Can anyone confirm this and / or show me how i would implement something like this in 5.2?</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>So I have discovered the issue. In <code>Illuminate/Routing/Router.php:834</code> the router tries to substitute implicit bindings. This happens BEFORE middleware and checks for the route for valid "uses". I have 0 idea how to work around this as it totally blows away what I am currently doing. Any suggestions?</p>