开发者

Zend Framework application with login and signup pages on one domain and other pages on the subdomain

We develop a zend framework application and want that signup pages and login pages were on our domain for example http://domain.com (now all pages are on http://domain.com) and other pages (where you have to be redirected after authentification) on subdomain: http://subdomain.domain.com. Could you please tell how to solve it? Thank you 开发者_开发知识库for any ideas.


I've done this kind of thing in a major ZF app of mine. It's a complicated 3-part question, but hopefully this will get you started in the right direction.

First is the session cookie. You'll need to use a wildcard domain parameter when you set the session cookie. In your Bootstrap or somewhere prior to when you would normally start your session, include a line such as:

Zend_Session::start(array('cookie_domain' => '.domain.com'));

Note the dot (".") prior to "domain.com". This makes the cookie applicable for domain.com as well as all subdomains under it.

Second is the URL's throughout your site. You can use the Redirector action helper's gotoUrl() method. At the end of an action method when you want to redirect the user, use a line like this:

$this->_redirector->gotoUrl('http://domain.com/somewhere/else');

Of course, you may want to assemble the URL string by other means such as storing the domain in a configuration parameter and concatenating the path using one of ZF's native methods of generating a URL path. See the documentation for Zend_Controller_Action_Helper_Redirector for more. You'll also need to be careful about all URL's on your site and make sure the right domain is included in each link.

Third is how your app interprets routes when subdomains are involved. There are a few ways to accomplish this, but one is to create a module within your app that corresponds to each subdomain you want to use. Then use a Controller Plugin to intercept ZF's normal routing mechanism and set the module name appropriately based on the subdomain. Something like:

class My_Controller_Plugin_RouteMySubdomains extends Zend_Controller_Plugin_Abstract {

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        $hostname = $request->getHttpHost();
        if (strlen($hostname) > strlen('domain.com')) {
            $moduleName = preg_replace("/\.domain\.com$/", '', $hostname);
            $request->setModuleName($moduleName);
        }
    }
}

You'll need to tell ZF to use this plugin. If you're using Zend_Application with an application.ini file for basic configuration, you'll need to add a line like this:

resources.frontController.plugins.routeMySubdomains = "My_Controller_Plugin_RouteMySubdomains"

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜