Salsa
Salsa

Reputation: 957

Database selection using route prefixing in cakephp 2.4?

I'm working on a system which may be used by different segments. Each will have it's own separate database, but all with the same functionalities. I'd like to have a prefix in my routes so I could select the database.

I found and tried to follow this suggestion: CakePHP - select database config based on route or url?

So using the above answer, I have this setup:

route.php:

App::uses('SystemSelector', 'Routing/Route');

Router::connect('/:sys/*', array(), array('routeClass' => 'SystemSelector'));

Router::connect('/:sys',             array('controller' => 'users', 'action' => 'login'));
Router::connect('/:sys/users/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/:sys/users/add',    array('controller' => 'users', 'action' => 'add'));

Router::connect('/:sys/tickets/index', array('controller' => 'tickets', 'action' => 'index'));
Router::connect('/:sys/tickets/view', array('controller' => 'tickets', 'action' => 'view'));
Router::connect('/:sys/tickets/add', array('controller' => 'tickets', 'action' => 'add'));

SystemSelector.php:

class SystemSelector extends CakeRoute {
    // see http://book.cakephp.org/view/1634/Custom-Route-classes
    public function parse($url) {
        $params = parent::parse($url);
        if (empty($params)) {
           return false;
        }

        switch ($params['sys']) {
            case 'test':
                DbSelector::$ds = 'test';
                break;
            case 'example':
                DbSelector::$ds = 'example';
                break;            
            default:
                throw new Exception('Bad route');
        }

        return false;
    }
}

and my AppModel.php:

class AppModel extends Model {
    public function __construct($id = false, $table = null, $ds = null) {
        $ds = DbSelector::$ds;
        parent::__construct($id, $table, $ds);
    }
}

The problem I'm having is that at my appController.php I have:

public $components = array(
        'Session',
        'DebugKit.Toolbar',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'My64',
                    'fields' => array('username' => 'user_login',
                                      'password' => 'user_senha')
                )
            ),
            'loginAction'    => array('controller' => 'users',   'action' => 'login'),
            'loginRedirect'  => array('controller' => 'tickets', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users',   'action' => 'login')
        )
    );

and the login redirect takes me to example.com/tickets/index , which I think I should make: example.com/:sys/tickets/index.

How do I pass the :sys prefix to this loginRedirect option? Also, how do I create links inside my views with the correct :sys prefix?

Or am I completely off?

Upvotes: 0

Views: 321

Answers (1)

ndm
ndm

Reputation: 60463

As far as I understand you are looking for the persist option, it defines which parameters should automatically be included when generating URLs. Also you might want to make use of the controller and action.

Example:

$options = array(
    'routeClass' => 'SystemSelector'
    'persist'    => array('sys')
);

Router::connect(
    '/:sys/',
    array('controller' => 'users', 'action' => 'login'),
    $options
);

Router::connect(
    '/:sys/:controller',
    array('action' => 'index'),
    $options
);

Router::connect(
    '/:sys/:controller/:action/*',
    array(),
    $options
);

For all matching routes this should automatically include the appropriate value passed for the sys parameter when generating URLs.

See also http://book.cakephp.org/2.0/en/development/routing.html#Router::connect

Upvotes: 2

Related Questions