alairock
alairock

Reputation: 1934

Routes in plugin with CakePHP

I have a plugin that needs to override the default route for /

The file I have attempting this is at APP/Plugin/Install/Config/routes.php

<?php
Router::connect('/', array('plugin' => 'install', 'controller' => 'installer', 'action' => 'index'));

Which does not work. I am also loading all plugins in my bootstrap. CakePlugin::loadAll();

Am I missing something?

[UPDATE. This file needs to override the main routes.php file in APP/Config/routes.php. Obviously updating the main routes file works and shows the right page, but I am trying to override this file and not modify it directly.]

Upvotes: 2

Views: 5902

Answers (3)

Kai
Kai

Reputation: 228

Routes, that are added later, will be overwriten by the route first defined.

e.g. the route '/' is usually defined in the app/config/routes.php if you want to overwrite that from your 'app/plugin/YOURPLUGIN/config/routes.php', you need to use 'Router::promote'.

See documentation on Router::promote

e.g. app/plugin/YOURPLUGIN/config/routes.php

Router::connect(
'/',
array(
    'plugin' => 'YOURPLUGIN',
    'controller' => 'YOURPLUGIN_CONTROLLER',
    'action' => 'index'
));
Router::promote();

This puts the route of the plugin before the original route for '/', thus matching it first.

Upvotes: 1

Daniel
Daniel

Reputation: 445

I haven't tried overriding the default route for a plugin - I have for plain controllers - but I think you'll need 'plugin'=>'install' or somesuch in your array.

Edit: This bit about plugins in the manual might apply, I think your loadAll should look something like this:

CakePlugin::loadAll(array(
    'Install' => array('routes' => true)
));

Upvotes: 2

dogmatic69
dogmatic69

Reputation: 7575

If you want to route to a plugin you should specify it, Cake will not guess what plugin you want.

Router::connect('/', array('plugin' => 'install', 'controller' => 'installer', 'action' => 'index'));

Upvotes: 1

Related Questions