3

This is not a real question, I need a confirmation to know if I understand what I'm studying (the routes of CakePHP).

I have the plugin MyPlugin. By default, all requests should be directed to the plugin, so I wish that the plugin name doesn't appear in the url.

For example:

/pages

should be resolved as:

['controller' => 'pages', 'action' => 'index', 'plugin' => 'MyPlugin']

The same should apply to the "admin" prefix.

For example:

/admin/pages

should be resolved as:

['controller' => 'pages', 'action' => 'index', 'plugin' => 'MyPlugin', 'prefix' => 'admin']

In short, you have to imagine as if the application (so except for MyPlugin) has no controller.

I studied routes (particularly this and this) and now I would like to know if this code is correct:

Router::defaultRouteClass('InflectedRoute');

Router::prefix('admin', function ($routes) {
    $routes->plugin('MeCms', ['path' => '/'], function ($routes) {
        $routes->fallbacks();
    });
});

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->fallbacks();
});

From my tests, this seems to work. But since the routes have changed a lot compared to CakePHP 2.x, I would like to have confirmation that you have understood.

Thanks.


EDIT

Thanks to PGBI, this code should be final:

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {
    Router::connect('/admin', ['controller' => 'Pages', 'action' => 'index', 'plugin' => 'MeCms', 'prefix' => 'admin']);

    $routes->prefix('admin', function ($routes) {
        $routes->fallbacks();
    });
    $routes->fallbacks();
});
Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36

1 Answers1

3

Yes that's correct. I think you could do shorter (to be tested, but you get the idea):

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->prefix('admin', function ($routes) {
        $routes->fallbacks();
    });
    $routes->fallbacks();
});

EDIT: To add a homepage to your admin section :

Router::scope('/', ['plugin' => 'MeCms'], function ($routes) {  
    $routes->prefix('admin', function ($routes) {
        $routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
        $routes->fallbacks();
    });
    $routes->fallbacks();
});

You don't need to repeat ['plugin' => 'MeCms'] or ["prefix" => "admin"] since it's already defined before.

PGBI
  • 700
  • 6
  • 18
  • Yeah @PGBI, it's work. But now: how to add an homepage for admin prefix? I have edited my question, by entering the code that *should* be final. What do you think about it? – Mirko Pagliai Apr 08 '15 at 13:08