如何将 www.example.com/admin 的请求重定向到 Zend 中的不同模块

发布于 2024-12-07 11:22:56 字数 3391 浏览 1 评论 0原文

我在应用程序中重定向请求时遇到问题。

我的规则:

  • employer.domain.com - 应指向雇主的页面 - 使用默认模块
  • employer.domain.com/panel/ - 应指向特定雇主的管理页面 - 使用仪表板模块
  • www.domain.com - 应指向聚合所有雇主的页面 - 使用默认 > 模块

我已经测试了很多不同的路线,但是当一条路线正常工作时,其他路线就会损坏。通常它仅适用于根路径,但是当我添加对某些控制器和操作的调用时 - 它会崩溃。也许我应该编写自定义控制器插件?你怎么认为?

这是我当前的配置。虽然很混乱,但也许这有助于发现一些愚蠢的错误。

// employer.domain.com/panel
$pathRoute_panel = new Zend_Controller_Router_Route(
    ':panel/:controller/:action/:id/',
    array(
        'panel' => '',
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index',
        'id' => '',
    ),
    array(
        'panel' => 'panel'
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));


// employer.domain.com - main employer page
$pathRoute_panel = new Zend_Controller_Router_Route(
    '',
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));


// domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index',
    ),
    $dispatcher,
    $request
);

$route = new Zend_Controller_Router_Route_Hostname($config['host']);
$router->addRoute('default', $route->chain($pathRoute));

// www.domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index',
    ),
    $dispatcher,
    $request
);

$route = new Zend_Controller_Router_Route_Hostname('www.'.$config['host']);
$router->addRoute('default_www', $route->chain($pathRoute));

编辑:这是我的解决方案:

// employer.domain.com
$pathRoute_panel = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));

// employer.domain.com/panel/
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:controller/:action/:id/',
    array(
        'panel' => 'panel',
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));

I have a problem with redirecting requests in my application.

My rules:

  • employer.domain.com - should point to a page of the employer - uses default module
  • employer.domain.com/panel/ - should point to a administration page of specific employer - uses dashboard module
  • www.domain.com - should point to page aggregating all employers - uses default module

I've tested a lot of different routes, but when one route is working, other get broken. Also often it works only for root paths, but when I add call to some controller and action - it crashes. Maybe I should write custom controller plugin? What do You think?

Here is my current configuration. It's a mess, but maybe it will help catching some silly mistake.

// employer.domain.com/panel
$pathRoute_panel = new Zend_Controller_Router_Route(
    ':panel/:controller/:action/:id/',
    array(
        'panel' => '',
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index',
        'id' => '',
    ),
    array(
        'panel' => 'panel'
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));


// employer.domain.com - main employer page
$pathRoute_panel = new Zend_Controller_Router_Route(
    '',
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));


// domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index',
    ),
    $dispatcher,
    $request
);

$route = new Zend_Controller_Router_Route_Hostname($config['host']);
$router->addRoute('default', $route->chain($pathRoute));

// www.domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index',
    ),
    $dispatcher,
    $request
);

$route = new Zend_Controller_Router_Route_Hostname('www.'.$config['host']);
$router->addRoute('default_www', $route->chain($pathRoute));

EDIT: This is my solution:

// employer.domain.com
$pathRoute_panel = new Zend_Controller_Router_Route_Module(
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));

// employer.domain.com/panel/
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:controller/:action/:id/',
    array(
        'panel' => 'panel',
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index',
        'id' => '',
    )
);

$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    ':employer.'.$config['host'],
    null,
    array(
        'employer' => '([a-z0-9]+)',
    )
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-12-14 11:22:56
// Enforce Subdomain usage
// Will match employer.domain.com/*
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    'employer.'.$config['host'] . '/*'
);


// Will match /panel/{id, required}/{controller, optional}/{action, optional}/{further parameters, optional}
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:id/:controller/:action/*',
    array(
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index'
    ),
    array(
        'id' => '\d+'
    )
);

// employer.domain.com - main employer page
// will match everything not matched before!
$pathRoute_page = new Zend_Controller_Router_Route(
    '*',
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index'
    )
);

// You can add several routes to one chain ;) The order does matter.
$subDomainRoute->chain($pathRoute_page);
$subDomainRoute->chain($pathRoute_panel);
$router->addRoute($subDomainRoute);

注释在代码中。您可以使用通配符 (*) 来进一步匹配任何内容!不需要 default_www 路由 - 这只是默认行为(默认路由现在将匹配除雇主之外的每个子域,因为它与 subDomainRoute 匹配)。

// Enforce Subdomain usage
// Will match employer.domain.com/*
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
    'employer.'.$config['host'] . '/*'
);


// Will match /panel/{id, required}/{controller, optional}/{action, optional}/{further parameters, optional}
$pathRoute_panel = new Zend_Controller_Router_Route(
    'panel/:id/:controller/:action/*',
    array(
        'module' => 'dashboard',
        'controller' => 'index',
        'action' => 'index'
    ),
    array(
        'id' => '\d+'
    )
);

// employer.domain.com - main employer page
// will match everything not matched before!
$pathRoute_page = new Zend_Controller_Router_Route(
    '*',
    array(
        'module' => 'default',
        'controller' => 'vcard',
        'action' => 'index'
    )
);

// You can add several routes to one chain ;) The order does matter.
$subDomainRoute->chain($pathRoute_page);
$subDomainRoute->chain($pathRoute_panel);
$router->addRoute($subDomainRoute);

Annotations are in the code. You can use the wildcard (*) to match anything further! There's no need of the default_www route - this's just the default behaviour (the default route now will match every subdomain but employer as it is matched by the subDomainRoute).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文