4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP3でフロントとバックエンドでControllerとTemplateをフォルダ分けするための設定

Last updated at Posted at 2017-01-17
cake version 3.3.11

cakePHP3で下記のようにフロントとバックエンドでController等を分けたいことありますよね?

 src/
  Controller/
   Front/
    ItemsController.php
    ...
   Admin/
    UsersController.php
    ...
  Template/
   Front/
    Items/index.ctp
    ...
   Admin/
    UsersController.php
    ...

cakephp3では、routesを修正することでできます。
※コメント文は削除してます。

config/routes.php
<?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

Router::defaultRouteClass(DashedRoute::class);

Router::scope('/', function ($routes) {
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    // 管理トップ用 ↓追加
    $routes->connect('/admin', ['controller' => 'Users', 'action' => 'index', 'prefix' => 'admin']);

    // これはコメントアウトする ↓修正
    //$routes->fallbacks(DashedRoute::class);

    // フロントは、Front/ へ ↓追加
    $routes->connect('/:controller', ['action' => 'index', 'prefix' => 'front'], ['routeClass' => 'DashedRoute']);
    $routes->connect('/:controller/:action/*', ['prefix' => 'front'], ['routeClass' => 'DashedRoute']);
});

// 管理用(トップ以外)ルーティング ↓追加
Router::prefix('admin', function ($routes) {
    $routes->fallbacks('DashedRoute');
});

Plugin::routes();
4
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?