title: CakePHP 3 routes when using separate Controller namespaces
body: <p>This question concerns routing (<code>config/routes.php</code>) in CakePHP 3.7.3</p>
  <p>I have an application which uses 3 Controllers. Two of these are within an 'Admin' namespace:</p>
  <ul>
  <li><code>Controller/Admin/ArticlesController.php</code></li>
  <li><code>Controller/Admin/UsersController.php</code></li>
  <li><code>Controller/ArticlesController.php</code></li>
  </ul>
  <p>I'm trying to achieve the following things:</p>
  <ol>
  <li><p>Make a &quot;shortcut&quot; URL <code>https://example.com/admin</code> that serves <code>Admin/UsersController::login()</code> - i.e. <code>https://example.com/admin/users/login</code></p>
  </li>
  <li><p>The function that shows pages on the frontend of my website is <code>ArticlesController::view()</code>. If I have a URL slug &quot;foo&quot; my page serves the content from the URL <code>https://example.com/articles/view/foo</code>. However I want to make this just <code>https://example.com/foo</code>.</p>
  </li>
  </ol>
  <p>In my <code>config/routes.php</code> I have configured Admin routing with this:</p>
  <pre><code>Router::scope('/', function (RouteBuilder $routes) {
  
      Router::prefix('admin', function ($routes) {
          // All routes here will be prefixed with `/admin`
          // And have the prefix =&gt; admin route element added.
          $routes-&gt;fallbacks(DashedRoute::class);
      });
  
      $routes-&gt;fallbacks(DashedRoute::class);
  
  });
  </code></pre>
  <p>This works - I can login at <code>https://example.com/admin/users/login</code>.</p>
  <p>To solve (1) I attempted to add the following:</p>
  <pre><code>Router::connect('/admin', ['controller' =&gt; 'Users', 'action' =&gt; 'login']);
  
  // The line above is immediately outside the existing code shown previously:
  Router::scope('/' ...
  </code></pre>
  <p>But this gives me an error:</p>
  <blockquote>
  <p>Error: AdminController could not be found.</p>
  <p>Error: Create the class AdminController below in file: <code>src/Controller/AdminController.php</code></p>
  </blockquote>
  <p>The Controller it's being asked to use is <code>Users</code> so I don't understand why it's asking for an <code>AdminController</code>?</p>
  <p>To solve (2) I attempted:</p>
  <pre><code>$routes-&gt;connect('/*', ['controller' =&gt; 'Articles', 'action' =&gt; 'view]);
  </code></pre>
  <p>However when attempting to access <code>https://example.com/foo</code> it is giving the following error:</p>
  <blockquote>
  <p>Error: FooController could not be found.</p>
  </blockquote>
  <p>Clearly this isn't what I'm trying to do - I'm expecting it to use the <code>Articles</code> Controller and <code>view</code> action I've specified in the array.</p>
  <p>In my two Admin Controllers (<code>Controller/Admin/ArticlesController.php</code> and <code>Controller/Admin/UsersController.php</code>) I have declared:</p>
  <pre><code>namespace App\Controller\Admin;
  </code></pre>
  <p>outside the class name, e.g. <code>class ArticlesController extends AppController </code></p>
  <p>For the non-Admin <code>ArticlesController</code> (<code>Controller/ArticlesController.php</code>) I have declared:</p>
  <pre><code>namespace App\Controller;
  </code></pre>
  <p>followed by the class name <code>class ArticlesController extends AppController</code></p>
  <p>This seems overly complex. Can anyone help?</p>