【问题标题】:Setting a variable depending on ServerName in CakePHP在 CakePHP 中根据 ServerName 设置变量
【发布时间】:2012-10-27 11:24:43
【问题描述】:

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

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

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

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

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

就像 tumblr 一样...

完成此任务的最佳方法是什么?

谢谢大家:)

【问题讨论】:

  • 您不能将记录指向具有路径的域(例如www.example.com/username)。您只能将其指向一个域,例如 www.example.comusername.example.com。我建议重新考虑您的路由并改用子域。这是另一个问题,但在我确定之前已经在这里回答了。这里还有一个简短的例子:book.cakephp.org/2.0/en/appendices/…

标签: php apache cakephp dns subdomain


【解决方案1】:

向用户表“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
    }
}

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多