1

我正在构建一个小应用程序,人们可以在其中构建某种投资组合。

目前,每个用户都可以使用 url 链接到他们的投资组合www.example.com/USERNAME并处理 CakePHP 中的所有内容,这是我正在使用的路由器规则:

Router::connect('/:user/:action/*', array('controller'=>'pages'), array('named' => array('user')));

所以链接是这样形成的:www.example.com/USERNAME/home或者www.example.com/USERNAME/contact

我的问题来了。我希望用户能够将他的域的 DNS 指向我的 IP 并更改其中的 URLwww.mypersonalsite.com/home等等www.mypersonalsite.com/contact

就像豆瓣一样...

实现这一目标的最佳方法是什么?

谢谢大家 :)

4

1 回答 1

0

向用户表“personal_domain”添加一个新字段

确保您的 Apache 已设置好,因此您的应用程序可用的站点会将所有访问服务器的域转发到您的 Cake 应用程序

在 app_controller 中添加类似这样的内容到 beforeFilter()

$domain = $_SERVER[ 'SERVER_NAME' ];
$sessionDomain = '';
if( $this->Session->check( 'User.personal_domain' ) )
{
    $sessionDomain = $this->Session->read( 'User.personal_domain' );
}

if( $domain != $sessionDomain )
{
    $User = ClassRegistry::init( 'User' );
    $domainUser = $User->find( 'first', array(
        'conditions' => array(
            'User.personal_domain' => $domain
        )
    ) );

    if( empty( $domainUser ) )
    {
        // whatever you usually do for users that don't have their own domain
    }
    else
    {
        $this->Session->write( 'User.personal_domain', $domainUser [ 'User' ][ 'personal_domain' ] );
        // whatever you usually do, and now you know which user is connecting
    // you should use this info with whatever auth method you use to make sure
    // only that user can login to that domain
    }
}
于 2012-11-16T07:41:11.797 回答