【问题标题】:How to add dynamic country name in all menu url in cake PHP?如何在 cake PHP 的所有菜单 url 中添加动态国家名称?
【发布时间】:2015-05-18 08:18:00
【问题描述】:

我想在所有菜单 url 中使用当前会话国家名称,这仅用于在 url 中显示目的

假设当前国家是北朝鲜

当前网址是http://localhost/project-name/products/business_verticals

我想要在

网址是http://localhost/project-name/north-corea/products/business_verticals

north-corea 仅用于显示页面。

我在此会话中有国家/地区名称 $_SESSION["csite"].... 我想将此会话值用于所有菜单 url。

【问题讨论】:

标签: cakephp


【解决方案1】:
Try:
Add code on routes.php
$currentPath = Router::getPaths(true);
$currentUrl = '';
if (!empty($currentPath->query['url'])) {
    $currentUrl = trim($currentPath->query['url']);
} elseif (!empty($currentPath->url)) {
    $currentUrl = trim($currentPath->url);
}

if (!empty($currentUrl)) {
    $currentUrlArr = explode('/', trim($currentUrl, "/"));
    if ($currentUrlArr[0] == 'north-corea') {
        Router::connect('/*', array('controller' => $currentUrlArr[1], 'action' => $currentUrlArr[2]));
    }
}

【讨论】:

    【解决方案2】:

    Cakephp 本身就支持它所谓的 url 前缀。它们被加载到 routes.php 配置文件中,然后可以在您的应用程序的其他地方使用。要像您的示例中那样构造一个带有给定前缀的 url,您可以这样做

    [ 'controller' => 'products', 'action' => 'business-verticals', 'north-corea' => true ]
    

    【讨论】:

    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2020-05-26
    相关资源
    最近更新 更多