数据库选择使用cakephp 2.4中的路由前缀? [英] Database selection using route prefixing in cakephp 2.4?

查看:246
本文介绍了数据库选择使用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.

我发现并试图遵循这个建议: CookiePHP - 根据路线或网址选择数据库配置?

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:

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:

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;
    }
}

和我的AppModel.php:

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);
    }
}

我遇到的问题是在我的appController .php我有:

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')
        )
    );

,登录重定向将我带到example.com/tickets/index,我想我应该:
example.com/:sys/tickets/index。

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

如何传递:sys前缀到这个loginRedirect选项?
此外,如何在我的视图中创建链接,使用正确的:sys前缀?

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?

还是我完全关闭?

推荐答案

据我所知,您正在寻找 persist 选项,它定义在生成URL时应自动包括哪些参数。也可以使用控制器操作

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.

示例:

$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
);

对于所有匹配的路由,这应该自动包括为 sys

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

另请参阅 http://book.cakephp.org/2.0/en/development/routing.html#Router::connect

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

这篇关于数据库选择使用cakephp 2.4中的路由前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆